tags:

views:

70

answers:

2

Hi

I've got a set and I want to find the largest number not greater than x in it. (something like lower_bound(x) ) how should i do it? Is there any predefined functions?

set<int> myset;
myset.insert(blahblahblah);
int y;
//I want y to be greatest number in myset not greater than x
+1  A: 

You can use upper_bound like this: upper_bound(x)--. Upper bound gives you the first element greater than x, so the element you seek is the one before that. You need a special case if upper_bound returns begin().

Space_C0wb0y
Thanks for that.
user12345
A: 

In addition to lower_bound there is also upper_bound C++ reference

The function returns an iterator to the first value that is strictly greater than yours. If it returns begin() then all of them are, otherwise subtract one from the resulting iterator to get the value you are looking for.

CashCow