How can I traverse/iterate an STL map?

前端 未结 6 624
栀梦
栀梦 2021-01-30 02:14

I want to traverse an STL map. I don\'t want to use its key. I don\'t care about the ordering, I just look for a way to access all elements it contains. How can I do this?

6条回答
  •  野性不改
    2021-01-30 02:50

    You can iterate map by using auto iterator.

    Code Snippet:

    #include
    using namespace std;
    
    int main()
    {
          ios::sync_with_stdio(false);
          map mp;
    
          mp["a"]=500;
          mp["b"]=200;
          mp["d"]=300;
          mp["c"]=400;
    
          for(auto it=mp.begin(); it != mp.end(); it++)
          {
             cout<first <<" : "<second<

提交回复
热议问题