C++

How to iterate over the map in C++

In this quick tutorial, we will see how to iterate over in map in C++. There are multiple ways to iterate over the map in C++. With newer versions of C++, there are more advanced ways to iterate over the map in C++. Let’s go through each one by one.

Using for loop with std::map

We have created a map named countryCapitalMap and inserted key-value pairs to it. We then use an iterator to do a for loop through the members of the map data structure.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;

int main() {

    // Initialize a map
    map<string, string>countryCapitalMap;
    countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
    countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
    countryCapitalMap.insert(pair<string, string>("France", "Paris"));

    // Iterate using iterator in for loop
    for (auto itr = countryCapitalMap.begin();
              itr != countryCapitalMap.end(); itr++) {

              std::cout << itr->first // Access key
                  << ':'
                  << itr->second // Access value
                  << std::endl;
    }
    return 0;
}
Output:

China:Beijing
France:Paris
Nepal:Kathmandu

As you can see, we have printed country:capital(key:value) using for loop.

If you notice, we have used auto type specifier for map iterator because of readability. You can use map::iterator explicitly as well.
Note: If you see the output, it is sorted by keys in ascending order. This is because std::map is a sorted associative container with supplied Comparator(version C++11 onwards). Since we did not provide any comparator, so C++ has used the default Comparator for string.

Using while loop with stp::map

We can also use a while loop instead of for loop.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;

int main() {
    // Initialize a map
    map<string, string> countryCapitalMap;
    // Insert different elements in map
    countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
    countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
    countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
    countryCapitalMap.insert(pair<string, string>("France", "Paris"));
   
   
    // Create a iterator for the map and Initialize with begin
    auto itr=countryCapitalMap.begin();
    // Iterate using iterator in while loop
    while (itr!=countryCapitalMap.end())
    {
        std::cout << itr->first // Access key
              << ':'
              << itr->second // Access value
              << std::endl;
        itr++;
    }
    return 0;
}

Output:

China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu

Using Range based for loop (C++11 version onwards)

If you are using C++11 version, then this is most elegant way to iterate over map in C++. You can avoid traditional cubersome loops and use this instead.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // Initialize a map
    map<string, string> countryCapitalMap;
    // Insert different elements in map
    countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
    countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
    countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
    countryCapitalMap.insert(pair<string, string>("France", "Paris"));

   
    // Iterate using iterator in for loop
    for (const auto &ele : countryCapitalMap) {
        cout <<ele.first << ":" << ele.second<<"\n";
    }
   
    return 0;
}
Output:

China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu

Using range-based for loop with key-values pairs(C++17 version onwards)

This version is supported from c++17 onwards and provides a more flexible way for iterating over the map. You can explicitly access key-values pair in map which provides even more readable solution.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // Initialize a map
    map<string, string> countryCapitalMap;
    // Insert different elements in map
    countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
    countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
    countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
    countryCapitalMap.insert(pair<string, string>("France", "Paris"));

   
    // Iterate using iterator in for loop
    for (const auto& [key, value] : countryCapitalMap) {
        cout << key << ":" << value << "\n";
    }
   
    return 0;
}
Output:

China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu

That’s all about how to iterate over the map in C++.

About the author

Arpit Mandliya

I am a passionate software developer and blogger. I love to write programming tutorials in my spare time. I have been blogging on java2blog.com and code2master.com for more than 8 years.