forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_3_set-test.cpp
More file actions
55 lines (43 loc) · 1.39 KB
/
5_3_set-test.cpp
File metadata and controls
55 lines (43 loc) · 1.39 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
// file: 5set-test.cpp
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int i;
int ia[5] = {0, 1, 2, 3, 4};
set<int> iset{ia, ia + 5};
cout << "size=" << iset.size() << endl;
cout << "3 count =" << iset.count(3) << endl;
iset.insert(3);
cout << "size=" << iset.size() << endl;
cout << "3 count =" << iset.count(3) << endl;
iset.insert(5);
cout << "size=" << iset.size() << endl;
cout << "3 count =" << iset.count(3) << endl;
iset.erase(1);
cout << "size=" << iset.size() << endl;
cout << "3 count =" << iset.count(3) << endl;
cout << "1 count =" << iset.count(3) << endl;
set<int>::iterator ite1 = iset.begin();
set<int>::iterator ite2 = iset.end();
for (; ite1 != ite2; ++ite1) {
cout << *ite1;
}
cout << endl;
// 使用STL算法find可以搜索元素,但不推荐
ite1 = find(iset.begin(), iset.end(), 3);
if (ite1 != iset.end())
cout << "3 found" << endl;
ite1 = find(iset.begin(), iset.end(), 1);
if (ite1 == iset.end())
cout << "1 not found" << endl;
// 关联式容器应使用专用的find函数搜索更有效率
ite1 = iset.find(3);
if (ite1 != iset.end())
cout << "3 found" << endl;
ite1 = iset.find(1);
if (ite1 == iset.end())
cout << "1 not found" << endl;
// *ite1 = 9; // 修改失败
}