title: Why is std::find implemented this way?
body: <p>I happen to run into the source of <code>std::find</code> and find it confusing to me. Basically it divides the count of items by 4 and do the compare 4 in each round:</p>
  
  <pre><code>template&lt;typename _RandomAccessIterator, typename _Tp&gt;
  _RandomAccessIterator
  __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
     const _Tp&amp; __val, random_access_iterator_tag)
  {
    typename iterator_traits&lt;_RandomAccessIterator&gt;::difference_type
  __trip_count = (__last - __first) &gt;&gt; 2;
  
    for (; __trip_count &gt; 0; --__trip_count)
  {
    if (*__first == __val)
      return __first;
    ++__first;
  
    if (*__first == __val)
      return __first;
    ++__first;
  
    if (*__first == __val)
      return __first;
    ++__first;
  
    if (*__first == __val)
      return __first;
    ++__first;
  }
  
    switch (__last - __first)
  {
  case 3:
    if (*__first == __val)
      return __first;
    ++__first;
  case 2:
    if (*__first == __val)
      return __first;
    ++__first;
  case 1:
    if (*__first == __val)
      return __first;
    ++__first;
  case 0:
  default:
    return __last;
  }
  }
  </code></pre>
  
  <p>I have no idea why it's done this way. Looks like some optimization. But I don't think this will take advantage of multi-core that easy. This is in a single thread anyway. </p>
  
  <p>Any ideas?</p>