Using the STL / set / multiset / multimap map, how can I find the first value greater than or equal to the search key?

Suppose I have a set of values ​​stored in std :: set:

{1, 2, 6, 8}

and I have a search key, say 3. I want to put 3 in a function and get the first value greater than or equal to 3, in which case I would like to get 6.

The find () function provided in map / set / multimap / will certainly return a final iterator for this case. Is there a similar search function that will return 6 in this case?

+3
source share
3 answers

: upper_bound(X) , , X. lower_bound(X), , X. , [lower_bound(X), upper_bound(X)) X.

+11

upper_bound.

map<int, int> mymap = { 1,2,6,8 };
map<int,int>::iterator i = mymap.upper_bound(3); // returns an iterator to the '6' element.
+3

lower_bound.

, lower_bound, -, .

, , ().

+2

Source: https://habr.com/ru/post/1697952/


All Articles