map红黑树映射节点为节点或链表
1 // map
2
3 #include<iostream>
4 #include<map>
5
6 using namespace std;
7
8 void main()
9 {
10 map<char*,int> mymap;
11 mymap["师长"] = 8;
12 mymap["旅长"] = 7;
13 mymap["团长"] = 6;
14 mymap["司令"] = 10;
15 mymap["军长"] = 9;
16 mymap["军长"] = 19;// 以军长为准 只有一个
17
18 cout << mymap["司令"] << endl;
19
20
21 cout << thismap[10] << endl;
22
23 for (auto i : mymap)
24 {
25 cout << i.first << " " << i.second << endl;
26 }
27
28
29 for (auto ib=mymap.begin().ie=mymap.end();ib!=ie;ib++ )
30 {
31 cout << (*ib).first << " " << (*ib).second << endl;
32 }
33
34
35
36
37 cin.get();
38 }
39
40 //------------------------------------------------------------------------
41
42
43 void main()
44 {
45
46
47 map<int,char*> thismap;
48 thismap[103] = "大侠103";
49 thismap[104] = "大侠104";
50 thismap[102] = "大侠102";
51 thismap[109] = "大侠109";
52 thismap[101] = "大侠101";
53
54 for (auto i : thismap)
55 {
56 cout << i.first << " " << i.second << endl;
57 }
58
59 for (auto ib=thismap.begin().ie=thismap.end();ib!=ie;ib++ )
60 {
61 cout << (*ib).first << " " << (*ib).second << endl;
62 }
63
64
65 cin.get();
66 }
67
68
69 //------------------------------------------------------------------------
70
71 struct strless
72 {
73 bool operator ()(const char *str1,const char *str2)
74 {
75 return (strcmp(str1,str2) < 0);// 对比字符串,返回值为布尔
76 }
77 };
78
79 void main()
80 {
81 map<char*,int,strless> mymap;
82 mymap["a师长"] = 8;
83 mymap["b旅长"] = 7;
84 mymap["xy团长"] = 6;
85 mymap["c司令"] = 10;
86 mymap["z军长"] = 9;
87 mymap["a军长"] = 9;
88 mymap["l军长"] = 19;
89
90 for (auto i : mymap)
91 {
92 cout << i.first << " " << i.second << endl;
93 }
94
95 cin.get();
96 }
97
98 //------------------------------------------------------------------------
99
100 // multimap
101
102 #include<iostream>
103 #include<map>
104 #include<cstring>
105
106 void main()
107 {
108 multimap<char*,int> mymap;
109
110 mymap.insert(pair<char*,int>("司令",10));
111 mymap.insert(pair<char*,int>("司令",11));
112 mymap.insert(pair<char*,int>("司令",12));
113 mymap.insert(pair<char*,int>("司令",13));
114 mymap.insert(pair<char*,int>("军令",12));
115 mymap.insert(pair<char*,int>("军令",13));
116
117 for (auto i : mymap)
118 {
119 cout << i.first << " " << i.second << endl;
120 }
121
122
123 for (auto ib=mymap.begin().ie=mymap.end();ib!=ie;ib++ )
124 {
125 cout << (*ib).first << " " << (*ib).second << endl;
126 }
127
128 cin.get();
129 }
130
131 //------------------------------------------------------------------------
132
133 void main()
134 {
135 multimap<int,char*> mymap;
136
137 mymap.insert(pair<char*,int>(10,"司令"));
138 mymap.insert(pair<char*,int>(11,"司令1"));
139 mymap.insert(pair<char*,int>(11,"司令2"));
140 mymap.insert(pair<char*,int>(11,"司令3"));
141 mymap.insert(pair<char*,int>(12,"司令"));
142 mymap.insert(pair<char*,int>(13,"军令"));
143 mymap.insert(pair<char*,int>(9,"军令"));
144
145 for (auto i : mymap)
146 {
147 cout << i.first << " " << i.second << endl;
148 }
149
150
151 for (auto ib=mymap.begin().ie=mymap.end();ib!=ie;ib++ )
152 {
153 cout << (*ib).first << " " << (*ib).second << endl;
154 }
155
156 auto it = mymap.equal_range(11);
157 for (auto ib=it.first,ie=it.second;ib!=ie;ib++)
158 {
159 cout << (*ib).first << " " << (*ib).second << endl;
160 }
161
162 cin.get();
163 }
164
165 //------------------------------------------------------------------------
166
167 struct strless
168 {
169 bool operator ()(const char *str1,const char *str2)
170 {
171 return (strcmp(str1,str2) < 0);// 对比字符串,返回值为布尔
172 }
173 };
174
175 void main()
176 {
177 multimap<char*,int,strless> mymap;
178
179 mymap.insert(pair<char*,int>("b司令",10));
180 mymap.insert(pair<char*,int>("b司令",11));
181 mymap.insert(pair<char*,int>("b司令",12));
182 mymap.insert(pair<char*,int>("c司令",13));
183 mymap.insert(pair<char*,int>("a军令",12));
184 mymap.insert(pair<char*,int>("z军令",13));
185
186
187 for (auto i : mymap)
188 {
189 cout << i.first << " " << i.second << endl;
190 }
191
192 cin.get();
193 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-14 17:39 Dragon-wuxl 阅读(269) 评论(0) 收藏 举报
浙公网安备 33010602011771号