c++中map的3中遍历方式
一、
说明:容器都有成员begin和end,其中begin成员复制返回指向第一个元素的迭代器,而end成员返回指向容器尾元素的下一个位置的迭代器,它是一个不存在的元素位置。
1.
map<int,int> map; map<char,int>::iterator it=map.begin(); while(it!=map.end()) { cout<<it->first<<" "<<it->second<<endl; it++; }
2.
map<int,int> map; for(auto i=map.begin();i!=map.end();i++) cout<<i->first<<" "<<i->second<<endl;
3.
map<int,int> map; for (auto& [key, value] : map) { cout<<key<<value<<endl; }
希望可以帮助你们。
2. map