C++ map简单运用

 

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 typedef pair<float, string> TIntStrPair;
 8 
 9 map <int, TIntStrPair> IIStrMap; //<int, <float,string>>
10 
11 map <string, string> StrStrMap; // <string,string>
12 
13 int main(int argc, char *argv[]) {
14 
15     map <int ,string> IntStrMap;
16     
17     IntStrMap.insert(pair<int,string>(1, "Tom"));
18     IntStrMap.insert(pair<int,string>(2, "Jimes"));
19     IntStrMap.insert(pair<int, string>(3, "Mary"));
20     IntStrMap.insert(make_pair(4, "Adom")); //use make_pair();
21 
22     map<int,string>::iterator it;
23 
24     for (it = IntStrMap.begin(); it != IntStrMap.end(); it++) {
25         cout << it->first << " " << it->second << endl;
26     }
27 
28 
29     cout << "-------------------------------" << endl;
30 
31     IIStrMap.insert(pair<int, TIntStrPair>(1, pair<float, string>(1.1, "Tom")));
32     IIStrMap.insert(pair<int, TIntStrPair>(2, pair<float, string>(2.1, "Fantex")));
33 
34         IIStrMap.insert(make_pair(3, make_pair(3.2, "Jimes")));//use make_pair()
35 
36     map<int, TIntStrPair>::iterator It;
37 
38     for (It = IIStrMap.begin(); It != IIStrMap.end(); It++) {
39         cout << It->first << " " << It->second.first << " " << It->second.second << endl;
40     }
41 
42 
43     cout << "----------------------------------" << endl;
44 
45     StrStrMap.insert(pair<string,string>("md5sum", "true"));
46     StrStrMap.insert(pair<string,string>("sigcheck", "false"));
47     StrStrMap.insert(pair<string, string>("ors_path", "/sdcard/ors"));
48 
49     StrStrMap.insert(make_pair("time_zone", "CTS"));// use make_pair();
50 
51     cout << "list the value " << endl;
52 
53     map<string, string>::iterator iter;
54 
55     for (iter = StrStrMap.begin(); iter != StrStrMap.end(); iter++) {
56         cout << iter->first << " = " << iter->second << endl;
57     }
58 
59     cout << "change the value " << endl;
60 
61     iter = StrStrMap.begin();
62 
63     iter = StrStrMap.find("md5sum");
64     iter->second = "false";
65 
66     cout << "list the vale after change it " << endl;
67 
68     for (iter = StrStrMap.begin(); iter != StrStrMap.end(); iter++) {
69         cout << iter->first << " = " << iter->second << endl;
70     }
71 
72 
73     cout << "----------------------------------------" << endl;
74 
75     cout << "erase the element " << endl;
76 
77     iter = StrStrMap.begin();
78 
79     iter = StrStrMap.find("md5sum");
80     StrStrMap.erase(iter);
81 
82         for (iter = StrStrMap.begin(); iter != StrStrMap.end(); iter++) {
83         cout << iter->first << " = " << iter->second << endl;
84     }
85 
86 
87     return 0;
88 
89 }

 

posted @ 2013-12-22 20:36  sndnvaps  阅读(452)  评论(0编辑  收藏  举报