posts - 183,  comments - 10,  trackbacks - 0
STL algorithm 中的 lower_bound and upper_bound

lower_bound 返回的结果 >= 参数 value
upper_bound 返回的结果 > 参数 value

具体明细可参见百科
lower_bound:http://baike.baidu.cn/view/4720650.htm
upper_bound:http://baike.baidu.cn/view/4163451.htm

测试代码:
 1 #include <iostream>
 2 #include <set>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> si;
 9     //cout << si.insert(3) << endl;
10     cout << *(si.insert(3).first) << endl;
11     
12     for (int i = 1; i <= 10; i += 2)
13     {
14         si.insert(i);
15     }
16     for (set<int>::const_iterator cit = si.begin(); cit != si.end(); ++cit)
17     {
18         cout << *cit << ' ';
19     }
20     cout << endl;
21     
22     cout << endl;
23     cout << *lower_bound(si.begin(), si.end(), 3<< endl;
24     cout << *lower_bound(si.begin(), si.end(), 4<< endl;
25     cout << *lower_bound(si.begin(), si.end(), 5<< endl;
26     cout << *lower_bound(si.begin(), si.end(), 6<< endl;
27     
28     cout << endl;
29     cout << *upper_bound(si.begin(), si.end(), 3<< endl;
30     cout << *upper_bound(si.begin(), si.end(), 4<< endl;
31     cout << *upper_bound(si.begin(), si.end(), 5<< endl;
32     cout << *upper_bound(si.begin(), si.end(), 6<< endl;
33     
34     system("PAUSE");
35 }
36 

输出:

3
1 3 5 7 9

3
5
5
7

5
5
7
7


posted on 2013-05-30 20:55 unixfy 阅读(607) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理