美文网首页c&c++
[C++]如何遍历map

[C++]如何遍历map

作者: 有我wa | 来源:发表于2020-03-11 11:35 被阅读0次


    map是STL的一个关联容器,它提供一对一的hash。

    第一个可以称为关键字(key),每个关键字只能在map中出现一次;

    第二个可能称为该关键字的值(value);

    我们了解到map之后就可以操作他了

    include<iostream>

    include<map>

    using namespace std;

    int main(){

        //先随便声明个map,声明他的迭代器

        map<int,int> m;

        for(int i = 0; i < 5; ++i){

            m[i] = i * 1000;

        }

        map<int,int>::iterator it;

        //第一种

        for(auto &t : m){

            cout<<t.first<<t.second<<endl;

        }

        //第二种

        for(iter = m.begin(); iter != m.end(); ++iter){

            cout<<iter->first<<iter->second<<endl;

        }

        //第三种

        while(iter != m.end()){

            cout<<iter->first<second<<endl;

            ++iter;

        }

    }

    相关文章

      网友评论

        本文标题:[C++]如何遍历map

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