Traversing a map (or unordered_map) in C++ STL

We can traverse map and unordered_map using following different ways.

Using a range based for loop

map

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to traverse a map using range 
// based for loop
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
  
    cout << "Element  Frequency" << endl;
    for (auto i : m)
        cout << i.first << "   " << i.second 
             << endl;
  
    return 0;
}

chevron_right


unordered_map

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to traverse a unordered_map using 
// range based for loop
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
  
    cout << "Element Frequency" << endl;
    for (auto i : m)
        cout << i.first << "    " << i.second
             << endl;
  
    return 0;
}

chevron_right



Output [NOTE: For unordered_map output rows can be
in any order]



Element  Frequency
1        4
2        1
3        2
4        1

 

Traversing using begin() and end()

map

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to traverse a map using iterators
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
  
    cout << " Element Frequency" << endl;
    for (auto i = m.begin(); i != m.end(); i++)
        cout << i->first << "      " << i->second
             << endl;
  
    return 0;
}

chevron_right


unordered_map

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to traverse a unordered_map 
// using iterators
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
  
    cout << " Element Frequency" << endl;
    for (auto i = m.begin(); i != m.end(); i++)
        cout << i->first << "       " << i->second 
             << endl;
  
    return 0;
}

chevron_right



Output [NOTE: For unordered_map output rows can be
in any order]

Element  Frequency
1        4
2        1
3        2
4        1

This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.