Iterate over map c++ – How to Iterate Over a Map in C++

Iterate over map c++: In the previous article, we have discussed about C++ : Map Tutorial -Part 2: Map and External Sorting Criteria / Comparator. Let us learn how to Iterate Over a Map in C++ Program.

Iterate Over a Map

Iterate through a map c++: In this article we will discuss 3 different ways to iterate over a map.

By using STL Iterator :

How to iterate through a map c++: Create an iterator of of std::map first and then initialize it to the beginning of the map. Then by incrementing the iterator upto last iterate over the map.

std::map<std::string, int>::iterator it = sampleMap.begin();

To access key from iterator

it->first

To access value from iterator

it->second

Let’s see the below program to understand it clearly.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>

int main() 
{
    std::map<std::string, int> sampleMap;
    // Inserting Element in map
    sampleMap.insert(std::pair<std::string, int>("A", 1));
    sampleMap.insert(std::pair<std::string, int>("B", 2));
    sampleMap.insert(std::pair<std::string, int>("C", 3));
    sampleMap.insert(std::pair<std::string, int>("D", 4));

    // Creating a map iterator 
    // iterator pointing to beginning of map
    std::map<std::string, int>::iterator it = sampleMap.begin();
    // Iterating over the map using Iterator till end of map.
    while (it != sampleMap.end())
    {
        // key accessed.
        std::string word = it->first;
        // Avalue accessed.
        int count = it->second;
        std::cout << word << " :: " << count << std::endl;
        // Incrementing the Iterator to point to next element
        it++;
    }
    return 0;
}
Output :

A :: 1
B :: 2
C :: 3
D :: 4

By using range based for loop :

Iterate over a map c++: Using range based for loop also we can iterate over the map.

Let’s see the below program to understand it clearly.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
int main() 
{
    std::map<std::string, int> sampleMap;
    // Inserting Element in map
    sampleMap.insert(std::pair<std::string, int>("A", 1));
    sampleMap.insert(std::pair<std::string, int>("B", 2));
    sampleMap.insert(std::pair<std::string, int>("C", 3));
    sampleMap.insert(std::pair<std::string, int>("D", 4));
    // Creating a map iterator 
    // iterator pointing to beginning of map
    std::map<std::string, int>::iterator it = sampleMap.begin();
    // Iterating over the map using Iterator till end of map.
    for (std::pair<std::string, int> item : sampleMap) {
        // key accessed.
        std::string word = item.first;
        // Avalue accessed.
        int count = item.second;
        std::cout << word << " :: " << count << std::endl;
    }
    return 0;
}
Output :

A :: 1
B :: 2
C :: 3
D :: 4

By using std::for_each and lambda function :

How to iterate through a map in c++: Using for each loop and lambda function also we can iterate over the map. Let’s see the below program to understand how we can achieve this.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
int main() 
{
    std::map<std::string, int> sampleMap;
    // Inserting Element in map
    sampleMap.insert(std::pair<std::string, int>("A", 1));
    sampleMap.insert(std::pair<std::string, int>("B", 2));
    sampleMap.insert(std::pair<std::string, int>("C", 3));
    sampleMap.insert(std::pair<std::string, int>("D", 4));
    // Creating a map iterator 
    // iterator pointing to beginning of map
    std::map<std::string, int>::iterator it = sampleMap.begin();
   // Iterating over the map using Iterator till end of map.
        std::for_each(sampleMap.begin(), sampleMap.end(),
                [](std::pair<std::string, int> element){
                    // key accessed.
                    std::string word = element.first;
                     // Avalue accessed.
                    int count = element.second;
                    std::cout<<word<<" :: "<<count<<std::endl;
        });
    return 0;
}
Output :

A :: 1
B :: 2
C :: 3
D :: 4