Suppose I have a set of values, stored in a std::set:
{1, 2, 6, 8}
and I have a search key, say, 3. I want to put 3 into a function and get the first value greater than or equal to 3, in this case I would want to get 6.
The find() function provided in map/set/multimap/and set will, of course, return the end iterator for this case. Is there a similar function to find that would return 6 in this case?
Yes: upper_bound(X)
returns an iterator pointing to the first element greater than X
. There is also a lower_bound(X)
function which returns an iterator pointing to the first element not less than X
. Thus, all of the elements in the half-open interval [lower_bound(X), upper_bound(X))
will be equal to X.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With