-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlower_bound.cpp
More file actions
59 lines (56 loc) · 1.74 KB
/
Copy pathlower_bound.cpp
File metadata and controls
59 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
using namespace std;
void test_lower_bound()
{
cout << "Begin test_lower_bound()\n";
vector<int> vec;
vec.push_back(100);
vec.push_back(10);
vec.push_back(20);
vec.push_back(20);
vec.push_back(200);
vec.push_back(300);
sort(vec.begin(), vec.end());
vector<int>::iterator itr_insert;
itr_insert = lower_bound(vec.begin(), vec.end(), 20);
cout << "find " << *itr_insert << " " << *(itr_insert+1)<< endl; //20 返回第一个大于等于value的iterator,如果没有,则返回end()
itr_insert = lower_bound(vec.begin(), vec.end(), 19);
cout << *itr_insert << endl; // 20
vec.insert(itr_insert,21); //在指定位置前插入
for (int i = 0; i < vec.size(); ++i)
cout << vec[i] << endl;
itr_insert = lower_bound(vec.begin(), vec.end(), 400);
if (itr_insert == vec.end()) cout << "end()\n";
cout << *itr_insert << endl; // 0
}
void test_upper_bound()
{
cout << "Begin test_upper_bound()\n";
vector<int> vec;
vec.push_back(100);
vec.push_back(10);
vec.push_back(20);
vec.push_back(20);
vec.push_back(21);
vec.push_back(200);
vec.push_back(300);
sort(vec.begin(), vec.end());
vector<int>::iterator itr_insert;
itr_insert = upper_bound(vec.begin(), vec.end(), 20);
cout << "find " << *itr_insert << endl; //20 返回第一个大于value的iterator,如果没有,则返回end()
itr_insert = upper_bound(vec.begin(), vec.end(), 19);
cout << *itr_insert << endl; // 20
vec.insert(itr_insert,21); //在指定位置前插入
for (int i = 0; i < vec.size(); ++i)
cout << vec[i] << endl;
itr_insert = upper_bound(vec.begin(), vec.end(), 400);
if (itr_insert == vec.end()) cout << "end()\n";
cout << *itr_insert << endl; // 0
}
int main()
{
test_lower_bound();
test_upper_bound();
return 0;
}