Perhaps I’m misunderstanding the technical definition of lower bound but I would expect if I had a set a = { 0, 3, 4 } and computed the a.lower_bound(2) that the result would be 0. I.e. I would expect std::set::lower_bound to be close to the mathematical concept of infimum
And yet the standard library defines it as the largest number not less than (effectively >=) x.
What is the reasoning behind this?
The “
[lower|upper]_bound” functions are meant to return a place in a set where you could insert a key that would not violate the ordering of the set. Because an iterator of an STL set points to before the next element, iflower_bound(2)returned an iterator to0, then inserting2would violate the order of your set, it would now be{2, 0, 3, 4}. Upper bound serves to show the last place you could insert without violating set order.This is most useful if your set may have duplicate key entries. Consider
{0, 3, 3, 4}.lower_bound(3)would return an iterator to here:{0, *, 3, 3, 4}, whileupper_bound(3)would return it here:{0, 3, 3, *, 4}.