For example I have set of values in std::set
:
{1, 2, 3, 5, 6}
And a search key, let it be 4, I want to find the first val. less than search key, 3 in this case, how to do it?
In Java there're functions greater()
, lower()
in TreeSet
Simply find the lower_bound for that key and then decrement it once.
set<int> a;
set<int>::iterator it = a.lower_bound(5);
if (it != a.begin()) {
it--;
cout << *it << endl;
} else {
cout << "No smaller element found!" << endl;
}
You can find a complete example here.
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