Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are using the <code>lower_bound</code> function for searching. Its purpose is a little different than that. <a href="http://www.cplusplus.com/reference/algorithm/lower_bound/" rel="nofollow">This</a> is what lower_bound does:</p> <blockquote> <p>Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value.</p> </blockquote> <p>And another definition from <a href="http://www.sgi.com/tech/stl/lower_bound.html" rel="nofollow">here</a>:</p> <blockquote> <p>Specifically, it returns the first position where value could be inserted without violating the ordering.</p> </blockquote> <p>So for example, if the thing you are looking for is not in the vector, it will return an iterator that points <strong>after</strong> the last item in the vector, and that iterator can't be dereferenced because it does not exist.</p> <p>Take a look at this example:</p> <pre><code>int myints[] = {10,20,30,30,20,10,10,20}; vector&lt;int&gt; v(myints,myints+8); // 10 20 30 30 20 10 10 20 vector&lt;int&gt;::iterator low; sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 low=lower_bound (v.begin(), v.end(), 60); // ^it will point here cout &lt;&lt; "lower_bound at position " &lt;&lt; int(low- v.begin()) &lt;&lt; endl; </code></pre> <p>As you can see from the output, the iterator will point to the 9th element in the vector (index 8). But the vector only has 8 elements (indexed 0-7). The explanation for this is that you can insert the new item in the vector at index 8 without violating the ordering.</p> <p>I think that what you really want is the <a href="http://www.cplusplus.com/reference/algorithm/find/" rel="nofollow"><code>find</code></a> function. Here is an example:</p> <pre><code>int myints[] = {10,20,30,30,20,10,10,20}; vector&lt;int&gt; v(myints,myints+8); // 10 20 30 30 20 10 10 20 vector&lt;int&gt;::iterator find_it1 = find(v.begin(), v.end(), 30); vector&lt;int&gt;::iterator find_it2 = find(v.begin(), v.end(), 80); if(find_it1 == v.end()) cout &lt;&lt; "30 not found" &lt;&lt; endl; else cout &lt;&lt; "30 found at position " &lt;&lt; int(find_it1 - v.begin()) &lt;&lt; endl; if(find_it2 == v.end()) cout &lt;&lt; "80 not found" &lt;&lt; endl; else cout &lt;&lt; "80 found at position " &lt;&lt; int(find_it2 - v.begin()) &lt;&lt; endl; </code></pre> <p>Here is the output:</p> <pre><code>30 found at position 2 80 not found </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload