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

An unordered_map is a hash table-based associative container provided by Standard Template Library in C++. It stores elements as key-value pairs. Let’s discuss some of the common ways to iterate over all the key-value pairs of an unordered_map.

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

Using a range-based for loop to iterate over an unordered_map in C++ is the simplest one. 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 <unordered_map>
#include <string>

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

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

    return 0;
}

Output:

That :: 4
How :: 3
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 unordered_map.

Using an Iterator

Iterators are a more traditional approach to iterate over elements in a container like unordered_map. They provide a way to traverse the container from beginning to end, accessing each element in sequence.

Let’s see the complete example,

#include <iostream>
#include <unordered_map>
#include <string>

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

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

    return 0;
}

Output:

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

In this loop, it is an iterator that starts from the beginning of the map (wordFrequency.begin()) and moves through each element until it reaches the end (wordFrequency.end()). The -> operator is used to access the key (it->first) and value (it->second) of the current element pointed to by the iterator. This method is versatile and powerful, especially when you need more control over the iteration process.

Using a Const Iterator

Const iterators are similar to regular iterators, but they are used when you want to iterate over a container without modifying its elements. This ensures that the data integrity of the map is maintained during iteration.

Here’s how you use a const iterator:

#include <iostream>
#include <unordered_map>
#include <string>

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

    // Iterate over unordered_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 particularly useful in functions or sections of code where maintaining the constness of the container is important.

Summary

Today we learned about different ways to loop over all key-value pairs of unordered_map in C++.

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.