About lower and upper bound in std::set

dear all,

I have a question about std::set::upper_bound() and std::set::lower_bound();

In the following code, I generate a set s that contains the values [0,10,20,30,40,50,60,70,80,90], and I retrieve the s.lower_bound(31) and s.upper_bound(31). I would expect to get, respectively, 30 and 40, but I get [40,40] as output.

Could someone explain me why?

#include<cstdio>
#include<set>

int main()
1
2
3
4
5
6
7
8
9
10
11
12
{
	std::set<int> s;
	for (int i=0; i<10; ++i)
		s.insert(i*10);
	
	std::set<int>::iterator it_lb = s.lower_bound(31);
	std::set<int>::iterator it_ub = s.upper_bound(31);
	
	printf("[%d, %d]\n", *it_lb, *it_ub);
	
	return 0;
}




Thanks in advance, Panecasareccio.
std::set::lower_bound
cplusplus reference pages wrote:
Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after).


http://www.cplusplus.com/reference/set/set/lower_bound/
Topic archived. No new replies allowed.