STL algorithm 涓殑 lower_bound and upper_bound
lower_bound 榪斿洖鐨勭粨鏋?>= 鍙傛暟 value
upper_bound 榪斿洖鐨勭粨鏋?> 鍙傛暟 value
鍏蜂綋鏄庣粏鍙弬瑙佺櫨縐?br />lower_bound錛?a >http://baike.baidu.cn/view/4720650.htm
upper_bound錛?a >http://baike.baidu.cn/view/4163451.htm
嫻嬭瘯浠g爜錛?
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
杈撳嚭錛?br />
3
1 3 5 7 9
3
5
5
7
5
5
7
7