From cppreference.com to std :: set :: lower_bound :
Return value
An iterator pointing to the first element that is no less than a key. If such an element is not found, an iterator of the past end is returned (see end () ).
In your case, since you do not have elements in your set that is not less (that is, greater than or equal to) than 11, the iterator of the past end is returned and assigned to it_l . Then in your line:
std::cout << *it_l << " " << *it_u << std::endl;
You put off this prototype iterator it_l : this behavior is undefined and can lead to something (1 in your test, 0 or any other value with another compiler, or the program may even crash).
Your lower bound should be less than or equal to the upper bound, and you should not dereference iterators outside of a loop or any other test environment:
#include <iostream> #include <set> using std::set; int main(int argc, char argv) { set<int> myset; set<int>::iterator it_l, it_u; myset.insert(9); myset.insert(10); myset.insert(11); it_l = myset.lower_bound(10); it_u = myset.upper_bound(10); while(it_l != it_u) { std::cout << *it_l << std::endl; // will only print 10 it_l++; } }
source share