Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Basic
  • Last Updated : 08 Jul, 2021

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
Element  Frequency
1   4
2   1
3   2
4   1

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

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
 Element Frequency
1      4
2      1
3      2
4      1

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

Iterating over a map by using STL Iterator:

By creating an iterator of std::map and initializing it to the starting of map and visiting upto the end of map we can successfully iterate over all the elements of map.

So, let’s see the below program to know how to do it.

C++




#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
int main()
{
    // Map created
    std::map<std::string, int> ExampleMap;
     
    // elements are inserted into map
    ExampleMap.insert(std::pair<std::string, int>("Sunday", 1));
    ExampleMap.insert(std::pair<std::string, int>("Monday", 2));
    ExampleMap.insert(std::pair<std::string, int>("Tuesday", 3));
    ExampleMap.insert(std::pair<std::string, int>("Wednesday", 4));
    ExampleMap.insert(std::pair<std::string, int>("Thursday", 5));
    ExampleMap.insert(std::pair<std::string, int>("Friday", 6));
    ExampleMap.insert(std::pair<std::string, int>("Saturday", 7));
     
    // map iterator created
    // iterator pointing to start of map
    std::map<std::string, int>::iterator it = ExampleMap.begin();
     
    // Iterating over the map using Iterator till map end.
    while (it != ExampleMap.end())
    {
        // Accessing the key
        std::string word = it->first;
        // Accessing the value
        int count = it->second;
        std::cout << word << " :: " << count << std::endl;
        // iterator incremented to point next item
        it++;
    }
    return 0;
}
Output



Friday :: 6
Monday :: 2
Saturday :: 7
Sunday :: 1
Thursday :: 5
Tuesday :: 3
Wednesday :: 4

 

Iterating over a map by using std::for_each and lambda function:

By using std::for and lambda function we can can successfully iterate over all the elements of map.

Where lambda function will be used as call back function and will receive each map entry.

So, let’s see the below program to know how to do it.

C++




#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
int main()
{
    // Map created
    std::map<std::string, int> ExampleMap;
     
    // elements are inserted into map
    ExampleMap.insert(std::pair<std::string, int>("Sunday", 1));
    ExampleMap.insert(std::pair<std::string, int>("Monday", 2));
    ExampleMap.insert(std::pair<std::string, int>("Tuesday", 3));
    ExampleMap.insert(std::pair<std::string, int>("Wednesday", 4));
    ExampleMap.insert(std::pair<std::string, int>("Thursday", 5));
    ExampleMap.insert(std::pair<std::string, int>("Friday", 6));
    ExampleMap.insert(std::pair<std::string, int>("Saturday", 7));
     
    // map iterator created
    // iterator pointing to start of map
    std::map<std::string, int>::iterator it = ExampleMap.begin();
     
    // Iterating over the map till map end.
        std::for_each(ExampleMap.begin(), ExampleMap.end(),
                [](std::pair<std::string, int> key_value)
                {
                    // Accessing the key
                    std::string word = key_value.first;
                    // Accessing the value
                    int count = key_value.second;
                    std::cout<<word<<" :: "<<count<<std::endl;
        });
    return 0;
}
Output
Friday :: 6
Monday :: 2
Saturday :: 7
Sunday :: 1
Thursday :: 5
Tuesday :: 3
Wednesday :: 4

This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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
Recommended Articles
Page :

Start Your Coding Journey Now!