1 # include <iostream>
2 # include <map>
3 # include <string>
4
5 using namespace STD;
6
7 int main (INT argc, char * argv [])
8 {
9 Map <string, int> mymap;
10
11 mymap ["bujiwu"] = 26;
12 mymap ["Swallow"] = 25;
13
14 STD: cout <"before Delete element:" <STD: Endl;
15 For (Map <string, int >:: iterator itor = mymap. Begin ();
16 itor! = Mymap. End (); itor ++)
17 {
18 STD: cout <itor-> first <"" <itor-> second <STD: Endl;
19}
20
21 For (Map <string, int >:: iterator itor = mymap. Begin ();
22 itor! = Mymap. End ();)
23 {
24 if (itor-> first = "bujiwu ")
25 {
26 mymap. Erase (itor ++); // in Windows, you can also use itor = mymap. Erase (itor), but not in Linux.
27}
28 else
29 {
30 itor ++;
31}
32}
33
34 STD: cout <"after Delete element:" <STD: Endl;
35 For (Map <string, int >:: iterator itor = mymap. Begin ();
36 itor! = Mymap. End (); itor ++)
37 {
38 STD: cout <itor-> first <"" <itor-> second <STD: Endl;
39}
40
41 return 0;
42}
43
44
The iterator method is available for vector and list on both Linux and Windows platforms.
Erase (iterator _ Where );
However, in Linux, map does not have the iterator erase (iterator _ Where) method );
Therefore, the above Code must be written as mymap in line 26th on Linux. erase (itor ++), but cannot write itor = mymap. erase (itor ). you can use either of the two methods in windows.