美文网首页
C++ Map 遍历方法

C++ Map 遍历方法

作者: 忻恆 | 来源:发表于2020-12-24 13:35 被阅读0次
    int main(){
        map<int, int> m;
        for (int i = 0; i < 10; i++){
            m[i] = i*10;
        }
        map<int,int>::iterator iter;
        iter = m.begin();
        while(iter != m.end()){
            cout << iter->first << "-" << iter->second << endl;
            iter++;
        }
        for (iter = m.begin();iter != m.end(); iter++){
            cout << iter->first << "-" << iter->second << endl;
        }
        for(auto &it : m){
            cout << it.first << "-" << it.second <<endl;
        }
        //反向遍历,只举一个例子
        iter = m.end();
        iter--;
        while(iter != m.begin()){
            cout << iter->first << "-" << iter->second << endl;
            iter--;
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ Map 遍历方法

          本文链接:https://www.haomeiwen.com/subject/qfrmnktx.html