title: STL std::find() C++
body: <p>In the below code I declared a vector as <code>{1,2,3,4,5}</code>.</p>
  
  <p>Using the STL <code>std::find()</code>, I am trying to find <code>5</code> in the vector ranging from <code>arr.begin()</code> to <code>arr.end()-1</code> or <code>arr.begin()</code> to <code>arr.begin()+4</code> which is the same range from <code>1</code> to <code>4</code>.</p>
  
  <p>But here for both, iterators are return pointing to <code>5</code>. Why is that since the range is only from <code>1</code> to <code>4</code>?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;array&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  int main () {
      vector&lt;int&gt; arr {1,2,3,4,5};
      // TEST
      for_each(arr.begin(), arr.begin()+4, [](const int &amp;x) { cerr &lt;&lt; x &lt;&lt; " "; }); cerr &lt;&lt; endl;
      for_each(arr.begin(), arr.end()-1, [](const int &amp;x) { cerr &lt;&lt; x &lt;&lt; " "; }); cerr &lt;&lt; endl;
  
      auto it1 {std::find(arr.begin(), arr.begin()+4, 5)};
      auto it2 {std::find(arr.begin(), arr.end()-1, 5)};
  
      if (it1 != arr.end())
          cout &lt;&lt; *it1 &lt;&lt; " Found!" &lt;&lt; endl;
      else
          cout &lt;&lt; "NOT Found!" &lt;&lt; endl;
  
      if (it2 != arr.end())
          cout &lt;&lt; *it2 &lt;&lt; " Found!" &lt;&lt; endl;
      else
          cout &lt;&lt; "NOT Found!" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>1 2 3 4 
  1 2 3 4 
  5 Found!
  5 Found!
  </code></pre>