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

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

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()



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

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
C++
Practice Tags :