title: why does std::search need forward iters
body: <p>my issue is identical to the thread below, I'm struggling to understand the answers given, or rather my code shouldn't work as it only uses input iterators ..but my func appears to work and behave identically to std::search ..so I'm at a loss and loathe to move on without understanding properly ...maybe if someone can suggest an input that will break my function but not the std::</p>
  
  <p>From <a href="https://stackoverflow.com/questions/3795114/why-do-i-need-a-forward-iterator-to-implement-my-customized-stdsearch">Why do I need a Forward Iterator to implement my customized std::search</a> :</p>
  
  <blockquote>
    <p>I am studying the book "Accelerated
    C++" from Koenig &amp; Moo.</p>
    
    <p>Exercise 8-2 ask me to implement on my
    own some templatized functions from
    &lt;algorithm&gt; and &lt;numeric&gt;, and to
    specify what kind of iterator does my
    implementation require.</p>
    
    <p>When trying to implement std::search,
    I determined that I need only "input"
    iterators.</p>
    
    <p>However, looking at the implementation
    of std::search installed with my
    compiler, I can see that they use
    "forward" iterators, but I cannot
    understand why, because there is no
    need to write, only to read, and input
    iterators meet the requirement.</p>
    
    <p>Can anybody here help me to understand
    this, please? Why would I need to use
    "forward" iterators to implement
    std::search?</p>
    
    <p>Thanks in advance.</p>
  </blockquote>
  
  <p>myfunction:</p>
  
  <pre><code>template &lt;class In&gt; 
  In search(  In begin, In end, In begin2, In end2 )
  {
      In found ;                      // iter: 1st element in pattern match(in content)
      In pattern_begin = begin2 ;     // iter: 1st element in search pattern.
      int flag = 0 ;                  // flag: partial match found?
  
      // search content for pattern 
      while (  begin &lt; end  ) {
  
          // if pattern-match fails ..reset vars
          // &amp; continue searching remaining content/elements
          if ( *begin != *begin2 ) {
  
              In ret ;                     
              begin2 = pattern_begin ;
              flag = 0 ;
              begin++ ;
  
  
          } else {
              // compare next element in pattern with next element in content.
              // if: 1st element of 'pattern' is found, store iter to it's pos
              // ..then if entire pattern is found, we can ret an iter to where it starts
              if ( flag == 0 ) { 
                  found = begin ;
                  flag = 1 ;
              }
              // inc iters to compare next elements in partial match
              begin++ ;
              begin2++ ;
          }
  
          // if: iter is 1-past end of search pattern
          // then entire pattern has been found 
          // return the iter to where it starts
          if( begin2 == end2 ) {  return found ;  }
  
      }
  
      // end of content reached, no complete pattern found
      // begin should? equal an iter 1-past the end of content
      return begin ;
  }
  </code></pre>
  
  <p>driver:</p>
  
  <pre><code>///* // Driver: custom::search(  b, e, b2, e2  ) 
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  //#include &lt;algorithm&gt;
  #include "library_algorithms.h"
  
  int main() {
  
      // init string test
      std::string content = "fo The fox  foxu jumped  foxe foxy " ;
      std::string search_pattern = "foxy" ;
  
      // func test on string
      std::string::iterator ret_iter = 
      custom::search(  content.begin(), content.end(), search_pattern.begin(), search_pattern.end()  ) ;
      //std::search(  content.begin(), content.end(), search_pattern.begin(), search_pattern.end()  ) ;
  
      // output
      if (  ret_iter != content.end()  ) {
  
          std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; search_pattern &lt;&lt; ": found at position: " &lt;&lt; int(  ret_iter - content.begin()  ) &lt;&lt; std::endl;
  
      } else { 
  
          std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; search_pattern &lt;&lt; ": ...not found" &lt;&lt; std::endl;
      }
  
  
  
  
      // Init vec test:
      // create content values in range:  10 20 30 &lt;......&gt; 9970 9980 9990
      std::vector&lt;int&gt; myvector;
      for (int i=1; i&lt;1000; i++) myvector.push_back(i*10);
  
      // create pattern values to search for
      std::vector&lt;int&gt; pattern ;
      pattern.push_back( 3730 ) ;
      pattern.push_back( 3740 ) ;
      pattern.push_back( 3750 ) ;
      pattern.push_back( 3760 ) ;
  
      // test: func on vector&lt;int&gt;
      std::vector&lt;int&gt;::iterator it;
      it = custom::search (  myvector.begin(), myvector.end(), pattern.begin(), pattern.end() );
  
      // output
      if (it!=myvector.end())
      std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; "pattern found at position " &lt;&lt; int(it-myvector.begin()) &lt;&lt; std::endl;
      else
      std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; "pattern not found" &lt;&lt; std::endl;
  
  
  
  
  
      return 0 ;
  
  }
  </code></pre>