【发布时间】:2012-05-20 23:43:33
【问题描述】:
我有以下完美的代码。
目的:给定一个数n,找出n的下一个和前一个数。
基于以下示例:如果 n = 50,那么我将分别得到 60 和 40。
我可以通过使用upper_bound 获得60。但是我如何获得 50 之前的数字我似乎找不到提供的算法来做到这一点。
set<int> myset;
set<int>::iterator it,itlow,itup;
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
itup=myset.upper_bound (50); //
cout << "upper_bound at position " << (*itup) << endl;
//output: 60
关于http://www.cplusplus.com/reference/stl/set/lower_bound/,它说upper_bound“返回一个迭代器,指向容器中不小于x的第一个元素”但我确定还有其他东西指向比较小于 x。
提前致谢! :)
【问题讨论】: