C++ map 遍历

#include <iostream>
#include <map>
using namespace std;

int main(){
    map<int,int> m;
    for (int i = 0; i < 10; i++){
        m[i] = i*i;
    }
    map<int,int>::iterator iter;
    iter = m.begin();
    while(iter != m.end()){
        cout << iter->first << "-" << iter->second << endl;
        iter++;
    }
    //for 循环
    for (iter = m.begin();iter != m.end(); iter++){
        cout << iter->first << "-" << iter->second << endl;
    }
    //auto 遍历
    for(auto &it : m){
        cout << it.first << "-" << it.second <<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/silentteller/p/10184641.html
今日推荐