forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_4_map-test.cpp
More file actions
40 lines (31 loc) · 1.04 KB
/
5_4_map-test.cpp
File metadata and controls
40 lines (31 loc) · 1.04 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
// file: 5map-test.cpp
#include <map>
#include <iostream>
#include <string>
using namespace std;
int main() {
map<string, int> simap;
simap[string("jjhou")] = 1;
simap[string("jerry")] = 2;
simap[string("jason")] = 3;
simap[string("jimmy")] = 4;
pair<string, int> value(string("david"), 5);
simap.insert(value);
map<string, int>::iterator simap_iter = simap.begin();
for (; simap_iter != simap.end(); ++simap_iter)
cout << simap_iter->first << ' '
<< simap_iter->second << endl;
int number = simap[string("jjhou")];
cout << number << endl;
map<string, int>::iterator ite1;
// 对于关联式容器,专用find函数比STL算法find效率更高
ite1 = simap.find(string("mchen"));
if (ite1 == simap.end())
cout << "mchen not found" << endl;
ite1 = simap.find(string("jerry"));
if (ite1 != simap.end())
cout << "jerry found" << endl;
ite1->second = 9; // 可以修改value
int number2 = simap[string("jerry")];
cout << number2 << endl;
}