1 #include<iostream>
2 #include<cstdlib>
3 using namespace std;
4 #include<set>
5 #include<string>
6
7
8 /*
9 3.8.6 set和multiset区别
10
11 set不可以插入重复数据,而multiset可以
12 set插入数据的同时会返回插入结果,表示插入是否成功
13 multiset不会检测数据,因此可以插入重复数据
14
15 3.8.7 pair对组的创建
16
17 功能:成对出现的数据,利用对组可以返回两个数据
18 pair<type, type> p (value1, value2);
19 pair<type, type> p = make_pair(value1, value2);
20 */
21
22
23 void test386()
24 {
25 set<int> s;
26 pair<set<int>::iterator, bool> ret= s.insert(10); //set的insert有返回值,为pair对组类型
27 if(ret.second)
28 {
29 cout << "插入成功" << endl;
30 }
31 else
32 {
33 cout << "插入失败" << endl;
34 }
35
36 ret = s.insert(10);
37 if(ret.second)
38 {
39 cout << "插入成功" << endl;
40 }
41 else
42 {
43 cout << "插入失败" << endl;
44 }
45
46
47 multiset<int> ms;
48 //multiset允许插入重复值
49 ms.insert(10);
50 ms.insert(10);
51 //multiset的insert有返回值,为迭代器iterator
52 for(multiset<int>::iterator it=ms.begin(); it!=ms.end(); it++)
53 {
54 cout << *it << " ";
55 }
56 cout << endl;
57 }
58
59
60 void test387()
61 {
62 pair<string, int> p("tom", 20);
63 cout << "name:" << p.first << " age:" << p.second << endl;
64
65 pair<string, int> p2 = make_pair("amy", 18);
66 cout << "name:" << p2.first << " age:" << p2.second << endl;
67 }
68
69
70 int main()
71 {
72 test386();
73 cout <<endl;
74 test387();
75
76 system("pause");
77 return 0;
78 }
