C ++ map traversal

The key is to declare iterator variables. The traversal method is similar to an array. You can use either a while loop or a for loop, but when using a for loop, the conditional operator cannot use < , but ! = Make a judgment.

Code:

#include<iostream>
#include<map>
using namespace std;
map<int,int>all;
int main()
{
	//先生成一些待处理的数据
    for(int i=0;i<100;i++)
        all[i]=i+1;
    //遍历map,i为迭代器类型
    for(map<int,int>::iterator i=all.begin();i!=all.end();i++)
        cout<<i->first<<" "<<i->second<<endl;
    return 0;
}
Published 15 original articles · praised 6 · visits 38

Guess you like

Origin blog.csdn.net/weixin_46165788/article/details/105517742