STL Set

View Code
 1 #include <iostream>
2 #include<set>
3
4
5 using namespace std;
6
7
8 template <typename Container>//原因是 a 和ma 的类型不一样,所以要做成模板函数 container还可以显示vector等
9 void PrintContents(const Container & c);
10
11
12 int main()
13 {
14
15
16 set<int> a;//set容器会自动进行排序
17 multiset<int> ma;
18
19
20 a.insert(60);
21 a.insert(-1);
22 a.insert(3000);
23 cout << "显示set里边的数据:" << endl;
24
25 /*set<int>::const_iterator i =a.begin();
26 while(i != a.end())
27
28 {
29
30 cout << *i << endl;
31 ++i;
32
33 }
34 cout << endl;
35 */
36
37 PrintContents(a);
38
39 ma.insert(a.begin(),a.end());//a里的数据全部插入ma
40 ma.insert(3000);
41
42 //计算ma里有几个3000,结果为2
43 cout << "multiset里有"<< ma.count(3000)<< "个3000" << endl;
44
45 cout << "显示multiset里面的数据:"<< endl;
46 /*multiset<int>::const_iterator i2 =ma.begin();
47 while(i2 != ma.end())
48
49 {
50
51 cout << *i2 << endl;
52 ++i2;
53
54 }
55
56 cout << endl;
57 */
58
59
60 PrintContents(ma);
61 return 0;
62
63
64
65 }
66
67 template <typename Container>
68 void PrintContents(const Container & c)
69 {
70 Container::const_iterator i =c.begin();
71 while(i != c.end())
72 {
73 cout << *i << endl;
74 ++i;
75
76 }
77
78 cout << endl;
79
80
81
82
83 }
posted @ 2012-03-27 23:28  uniquews  阅读(126)  评论(0编辑  收藏  举报