This is the 6th Part of Tutorial Series on map. In this tutorial, we will learn how to iterate over the elements of a map in C++.

A map is a binary search tree-based associative container provided by Standard Template Library in C++. It stores elements as key-value pairs in a sorted order. Let’s discuss some of the common ways to loop over all the key-value pairs of a map.

Using a Range-Based For Loop (C++11 and later)

Range-Based For Loop was introduced in C++11 and using it to iterate over a map in C++ is the simplest method. This method is particularly useful when you need to process each key-value pair and prefer a concise and straightforward syntax. Let’s see the complete example,

#include <iostream>
#include <map>
#include <string>

int main() 
{
    std::map<std::string, int> wordFrequency = {
                                                    {"Why", 1},
                                                    {"Where", 2},
                                                    {"How", 3},
                                                    {"That", 4}
                                                };

    // Iterate over map in C++
    for (const auto& pair : wordFrequency) 
    {
        std::cout << pair.first << " :: " << pair.second << std::endl;
    }

    return 0;
}

Output:

How :: 3
That :: 4
Where :: 2
Why :: 1

In this example, the for loop iterates over each element of the wordFrequency map. Each element is accessed as a std::pair, where pair.first is the key, and pair.second is the value. This method is very efficient for reading and processing all elements in the map.

Using an Iterator

Iterators provide a traditional approach to iterate over elements in a container like map. They enable traversal of the container from beginning to end, accessing each element in sequence.

Let’s see the complete example,

#include <iostream>
#include <map>
#include <string>

int main() 
{
    std::map<std::string, int> wordFrequency = {
                                                    {"Why", 1},
                                                    {"Where", 2},
                                                    {"How", 3},
                                                    {"That", 4}
                                                };

    // Iterate over map in C++
    for (auto it = wordFrequency.begin(); it != wordFrequency.end(); ++it) 
    {
        std::cout << it->first << " :: " << it->second << std::endl;
    }

    return 0;
}

Output:

How :: 3
That :: 4
Where :: 2
Why :: 1

Using a Const Iterator

Const iterators are used when you need to iterate over a container without modifying its elements. They ensure data integrity during iteration.

Here’s how you use a const iterator:

#include <iostream>
#include <map>
#include <string>

int main() 
{
    std::map<std::string, int> wordFrequency = {
                                        {"Why", 1},
                                        {"Where", 2},
                                        {"How", 3},
                                        {"That", 4} };

    // Iterate over map in C++
    for (auto it = wordFrequency.cbegin(); it != wordFrequency.cend(); ++it)
    {
        std::cout << it->first << " :: " << it->second << std::endl;
    }

    return 0;
}

In this code, wordFrequency.cbegin() and wordFrequency.cend() provide const iterators to the beginning and end of the map, respectively. The const iterator it allows you to read the key-value pairs but prevents any modification to the elements. This approach is useful in scenarios where maintaining the constness of the container is critical.

Summary

Today we learned about different ways to loop over all key-value pairs of a map in C++. Unlike unordered_map, a map maintains a sorted order based on keys, which is reflected in the order of iteration.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.