Skip to content
Related Articles

Related Articles

Traversing a map (or unordered_map) in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 23 Sep, 2017

We can traverse map and unordered_map using following different ways.

Using a range based for loop

map




// 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;
}


unordered_map




// 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;
}



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




// 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;
}


unordered_map




// 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;
}



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 C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :