Traversal of map in STL

Reprinted from: http://www.cnblogs.com/kaitoex/p/6081980.html

map is very useful as a mapping container in STL, let's talk about map traversal.

map.first is the key value, map.second is the value value, the key cannot be modified, and the value can be modified.

Define an iterative pointer iter to point to the map to traverse the map.

copy code
 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     map<string,int>M;
10     M["Kaito"]=1;
11     M["Aoko"]=2;
12     M["Shinichi"]=3;
13     M["Lan"]=4;
14     map<string,int>::iterator iter;//定义一个迭代指针iter
15     for(iter=M.begin(); iter!=M.end(); iter++)
16         cout<<iter->first <<"->"<<iter->second<<endl;
17     return 0;
18 }
copy code

operation result:

We can see that the map automatically sorts the key values ​​in ascii code order, rather than in the input order.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692143&siteId=291194637