【转】容器 C++ set和map

转载地址:http://blog.csdn.net/volkswageos/article/details/6020744

容器 C++ set和map

 set, multiset
set和multiset会根据特定的排序准则自动将元素排序,set中元素不允许重复,multiset可以重复。

因为是排序的,所以set中的元素不能被修改,只能删除后再添加。

向set中添加的元素类型必须重载<操作符用来排序。排序满足以下准则:
1、非对称,若A<B为真,则B<A为假。
2、可传递,若A<B,B<C,则A<C。
3、A<A永远为假。

set中判断元素是否相等:
if(!(A<B || B<A)),当A<B和B<A都为假时,它们相等。



map, multimap
map和multimap将key和value组成的pair作为元素,根据key的排序准则自动将元素排序,map中元素的key不允许重复,multimap可以重复。

因为是排序的,所以map中元素的key不能被修改,只能删除后再添加。key对应的value可以修改。

向map中添加的元素的key类型必须重载<操作符用来排序。排序与set规则一致。

  1 初始化
2
3 class A
4 {
5 public:
6 bool operator < (const A&) const
7 {
8 cout<<"A::operator <"<<endl;
9 return false;
10 }
11 };
12
13 //自定义的函数对象
14 template<typename T>
15 class FunObj
16 {
17 public:
18 FunObj():status(true){}
19 FunObj(int):status(false){}
20 bool operator ()(const T&, const T&)
21 {
22 cout<<boolalpha<<status<<endl;
23 return status;
24 }
25 private:
26 bool status;
27 };
28
29 int main()
30 {
31 set<A> sNone; //使用A类型的操作符<作为排序标准
32 set<A, FunObj<A>> sDefault; //使用函数对象作为排序标准,函数对象由默认构造函数构造
33 set<A, FunObj<A>> sSpecific((FunObj<A>(1))); //使用函数对象作为排序标准,函数对象由指定构造函数构造
34
35 sNone.insert(A());
36 sNone.insert(A()); //call A::operator <
37
38 sDefault.insert(A());
39 sDefault.insert(A()); //FunObj<A>::operator (), constructed by FunObj()
40
41 sSpecific.insert(A());
42 sSpecific.insert(A()); //FunObj<A>::operator (), constructed by FunObj(int)
43
44 return 0;
45 }
46
47
48
49
50 添加、删除元素
51 set<A> si;
52 pair<set<A>::iterator, bool> p = si.insert(A()); //返回pair,第二个值表示是否插入成功
53 cout<<boolalpha<<p.second<<endl;
54
55 set<A>::iterator it = si.insert(si.begin(), A()); //从指定位置开始查找,返回新元素的位置,
56
57 si.erase(A()); //删除值与参数相等的元素,返回删除元素个数
58 si.erase(si.begin()); //删除指定位置的元素,无返回值
59
60 si.clear(); //清空set
61
62 //map的insert对象是pair
63 map<A, A> m;
64 A a,b;
65 m.insert(map<A,A>::value_type(a,b)); //map<>::value_type()
66 m.insert(pair<A,A>(a,b)); //pair
67 m.insert(make_pair(a,b)); //make_pair()
68
69 其它操作
70 cout<<si.count(4)<<endl; //count返回值与指定参数相等的元素个数
71
72 set<int>::iterator it = si.find(3); //find返回第一个值与指定参数相等的位置,若没有,则返回end
73
74 it = si.lower_bound(3); //lower_bound返回第一个可以安插新元素的位置,即set中第一个>=新元素的位置
75 it = si.upper_bound(3); //upper_bound返回最后一个可以安插新元素的位置,即set中第一个>新元素的位置
76 pair<set<int>::iterator, set<int>::iterator> p
77 = si.equal_range(3); //equal_range返回set中元素与参数值相等的区间
78
79
80
81
82
83
84
85 // test.cpp : Defines the entry point for the console application.
86 //
87
88 #include "stdafx.h"
89 #include <vector>
90 #include <iostream>
91 #include <string>
92 #include <list>
93 #include <algorithm>
94 #include <map>
95 #include <set>
96
97 // 自定义数据的插入
98 struct student{
99 char name[20];
100 int age;
101 char city[20];
102 char phone[20];
103 };
104
105 // 这里采用函数对象的方式设置,与set中有不同,key设置为city,注意应设置为public
106 class stuCmp{
107 public:
108 stuCmp()
109 {
110
111 }
112 stuCmp(int d)
113 {
114
115 }
116 bool operator()(const student& a,const student& b) const{
117 return strcmp(a.city,b.city)<0;
118 }
119 };
120
121
122
123 int _tmain(int argc, _TCHAR* argv[])
124 {
125 using namespace std;
126 student stu1={"童进",23,"长沙","XXX"};
127 student stu2={"老大",28,"武汉","XXX"}; //老大,你成熟了5岁,哈哈
128 student stu3={"饺子",23,"福州","XXX"};
129 // multiset<student,stuCmp> b;
130 multiset<student,stuCmp> b(stuCmp(1));
131 b.insert(stu1);
132 b.insert(stu2);
133 b.insert(stu3);
134 // 武汉同学最多,只是现在大家又都天各一方
135 student stu4={"小芳",23,"武汉","XXX"};
136 student stu5={"黄庆",23,"武汉","XXX"};
137 student stu6={"英俊",23,"武汉","XXX"};
138 b.insert(stu4);
139 b.insert(stu5);
140 b.insert(stu6);
141
142 for(multiset<student,stuCmp>::iterator i=b.begin();i!=b.end();i++){
143 cout<<i->name<<endl;
144 }
145
146 student key={"",99,"武汉","XXX"};
147 cout<<"武汉朋友数目:"<<b.count(key)<<endl;
148 cout<<"武汉的第一个朋友:"<<b.lower_bound(key)->name<<endl;
149 cout<<"武汉最后一个朋友:"<<(--b.upper_bound(key))->name<<endl; // 这里武汉是最后的,再大于这个键值,就会返回end()指向头节点,所以--
150 for(multiset<student,stuCmp>::reverse_iterator j=b.rbegin();j!=b.rend();j++){
151 cout<<j->name<<endl;
152 }
153
154 // 验证set中的猜测,此时键值为city
155 student test={"路人甲",99,"武汉","XXX"};
156 multiset<student,stuCmp>::iterator v=b.find(test); //返回第一个与键值相等的迭代器
157 cout<<"寻找武汉的路人甲:"<<v->name<<endl;
158
159 // 元素搜索
160 cout<<"搜索武汉的朋友们:"<<endl;
161 pair<multiset<student,stuCmp>::iterator,multiset<student,stuCmp>::iterator> p=b.equal_range(test);
162 for(multiset<student,stuCmp>::iterator k=p.first;k!=p.second;k++){
163 cout<<k->name<<endl;
164 }
165 return 0;
166 }



原文地址:https://www.cnblogs.com/wainiwann/p/2371206.html