How can I traverse/iterate an STL map?

前端 未结 6 535
栀梦
栀梦 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 03:09

    Using for with auto for C++11 and above usage

    map map_variable; //you can use any data type for keys, as well as value
    
    for(auto &x:map_variable)
    { 
        cout<

    The newer format of for using auto was introduced in C++11

    To give it functionality like some higher level languages like python

    Where there was already an implementation of such type of iteration

    P.S. : map variable keeps values sorted, so when iterating you will get keys in sorted order

提交回复
热议问题