一种map容器遍历的方法

遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历。下面举个例子就知道了。

map<string,string> mp;
string a = "abc",b = "123";
mp.insert(pair<string,string> (a,b));
a = "cde";
mp.insert(pair<string,string> (a,b));

for(map<string,string>::iterator iter = mp.begin();iter != mp.end();++iter)
cout<<iter->first<<" "<<iter->second<<endl;

就是说map的迭代器里面有两个元素,first是表示键,second表示值,知道这个就可以遍历了。

 

posted @ 2014-10-07 14:26  xiaxiaosheng  阅读(837)  评论(0编辑  收藏  举报