map和unordered_map的遍历方法是相同的,不过遍历结果,map是有序的,unoredred_map遍历是无序的。 std:map 是个有序的关系容器,其完整原型如下:
template<
class Key,
class T,
class Compare=std::less<Key>,
class Allocator=std::allocator<std::pair<const Key,T>>
>class map;
键值对会根据键来用 Compare 排序,具体到 std::map<int, int> 就是:
map<int,int,std::less<int>>
C++11 map和unordered_map遍历方法
一、迭代器 iterator
for(map<int,int>::iterator it=mp.begin();it!=mp.end();++it)
cout<<it->first<<"--"<<it->second<<"\t";