title: Binary search in std::vector
body: <p>I am trying to look for the position of vector elements into another vector. Here i am interested to use an implementation as fast as <code>binary search</code>. I have different vectors of length 1 million or more, so i am trying to achieve something faster.</p>
  
  <p><strong>Following situations in my case:</strong></p>
  
  <p><strong>1)</strong> <code>vector</code> in which i am searching is sorted.</p>
  
  <p><strong>2)</strong> The element i am searching for will always be there i.e i don't have a case of <code>not found</code>, and i would like to get the index of vector elements in a faster way. </p>
  
  <p>I tried the following code to get the index of vector elements.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  template&lt;class Iter, class T&gt;
  Iter binary_find(Iter begin, Iter end, T val)
  {
      Iter i = std::lower_bound(begin, end, val);
      return i;
  }
  
  int main() {
      std::vector&lt;std::string&gt; values = {"AAAAAA","AB", "AD" ,"BCD","CD", "DD" };
      std::vector&lt;std::string&gt; tests = {"AB", "CD","AD", "DD"};
      for(int i=0 ; i &lt; tests.size(); i++) {
          int pos = binary_find(values.begin(), values.end(), tests.at(i))- values.begin();
      std::cout &lt;&lt; tests.at(i) &lt;&lt; " found at: " &lt;&lt; pos &lt;&lt;std::endl;
      }
      return 0;
  }  
  </code></pre>
  
  <p><strong>I would like to know if the code matches with the binary search implementation.??</strong></p>
  
  <p><strong>Is there a faster way to get the index of vector elements?</strong></p>
  
  <p><strong>Any further suggestions to improve this code.</strong></p>