How to Iterate Over a Map of Maps in C++ - Step by Step Examples



How to Iterate Over a Map of Maps in C++ ?

Answer

To iterate over a map of maps in C++, you can use nested loops. The outer loop iterates over the outer map, and the inner loop iterates over the inner map.



✐ Examples

1 Iterating Over a Map of Maps with Integer Keys and Values

We can iterate over a map of maps in C++ where both the outer and inner maps use integers for keys and values. This example demonstrates using nested loops to print each key-value pair.

For example,

  1. We start by including the necessary headers: #include <iostream> and #include <map>.
  2. We declare and initialize a map of maps named outerMap.
  3. We use a nested loop to iterate over the outer and inner maps and print each key-value pair.

C++ Program

#include <iostream>
#include <map>

int main() {
    // Declare and initialize a map of maps
    std::map<int, std::map<int, int>> outerMap = {
        {1, {{1, 10}, {2, 20}, {3, 30}}},
        {2, {{4, 40}, {5, 50}, {6, 60}}}
    };

    // Iterate over the map of maps
    std::cout << "Iterating over map of maps with integer keys and values:" << std::endl;
    for (const auto& outerPair : outerMap) {
        std::cout << "Outer key: " << outerPair.first << std::endl;
        for (const auto& innerPair : outerPair.second) {
            std::cout << "  Inner key: " << innerPair.first << ", value: " << innerPair.second << std::endl;
        }
    }

    return 0;
}

Output

Iterating over map of maps with integer keys and values:
Outer key: 1
  Inner key: 1, value: 10
  Inner key: 2, value: 20
  Inner key: 3, value: 30
Outer key: 2
  Inner key: 4, value: 40
  Inner key: 5, value: 50
  Inner key: 6, value: 60

2 Iterating Over a Map of Maps with Mixed Key and Value Types

We can iterate over a map of maps in C++ where the outer map uses strings for keys and the inner maps use integers for keys and strings for values. This example demonstrates using nested loops to print each key-value pair.

For example,

  1. We start by including the necessary headers: #include <iostream> and #include <map>.
  2. We declare and initialize a map of maps named outerMap with mixed key and value types.
  3. We use a nested loop to iterate over the outer and inner maps and print each key-value pair.

C++ Program

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

int main() {
    // Declare and initialize a map of maps
    std::map<std::string, std::map<int, std::string>> outerMap = {
        {"first", {{1, "one"}, {2, "two"}, {3, "three"}}},
        {"second", {{4, "four"}, {5, "five"}, {6, "six"}}}
    };

    // Iterate over the map of maps
    std::cout << "Iterating over map of maps with mixed key and value types:" << std::endl;
    for (const auto& outerPair : outerMap) {
        std::cout << "Outer key: " << outerPair.first << std::endl;
        for (const auto& innerPair : outerPair.second) {
            std::cout << "  Inner key: " << innerPair.first << ", value: " << innerPair.second << std::endl;
        }
    }

    return 0;
}

Output

Iterating over map of maps with mixed key and value types:
Outer key: first
  Inner key: 1, value: one
  Inner key: 2, value: two
  Inner key: 3, value: three
Outer key: second
  Inner key: 4, value: four
  Inner key: 5, value: five
  Inner key: 6, value: six

3 Iterating Over a Map of Maps with String Keys and Double Values

We can iterate over a map of maps in C++ where both the outer and inner maps use strings for keys and doubles for values. This example demonstrates using nested loops to print each key-value pair.

For example,

  1. We start by including the necessary headers: #include <iostream> and #include <map>.
  2. We declare and initialize a map of maps named outerMap with strings for keys and doubles for values.
  3. We use a nested loop to iterate over the outer and inner maps and print each key-value pair.

C++ Program

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

int main() {
    // Declare and initialize a map of maps
    std::map<std::string, std::map<std::string, double>> outerMap = {
        {"group1", {{"A", 1.1}, {"B", 2.2}, {"C", 3.3}}},
        {"group2", {{"D", 4.4}, {"E", 5.5}, {"F", 6.6}}}
    };

    // Iterate over the map of maps
    std::cout << "Iterating over map of maps with string keys and double values:" << std::endl;
    for (const auto& outerPair : outerMap) {
        std::cout << "Outer key: " << outerPair.first << std::endl;
        for (const auto& innerPair : outerPair.second) {
            std::cout << "  Inner key: " << innerPair.first << ", value: " << innerPair.second << std::endl;
        }
    }

    return 0;
}

Output

Iterating over map of maps with string keys and double values:
Outer key: group1
  Inner key: A, value: 1.1
  Inner key: B, value: 2.2
  Inner key: C, value: 3.3
Outer key: group2
  Inner key: D, value: 4.4
  Inner key: E, value: 5.5
  Inner key: F, value: 6.6

Summary

In this tutorial, we learned How to Iterate Over a Map of Maps in C++ language with well detailed examples.




More C++ Maps Tutorials

  1. How to create an Empty Map in C++ ?
  2. How to create a Map with Initial Key-Value Pairs in C++ ?
  3. How to Print a Map in C++ ?
  4. How to Add a Key-Value Pair to a Map in C++ ?
  5. How to Set a Default Value for a Key in a Map in C++ ?
  6. How to Update the Value for a Key in a Map in C++ ?
  7. How to Check if a Map is Empty in C++ ?
  8. How to Check if a Key Exists in a Map in C++ ?
  9. How to Check if a Value Exists in a Map in C++ ?
  10. How to Get the Value Associated with a Key in a Map in C++ ?
  11. How to Remove a Key-Value Pair from a Map in C++ ?
  12. How to Remove Key-Value Pairs from a Map Based on Values in C++ ?
  13. How to Clear All Key-Value Pairs from a Map in C++ ?
  14. How to Iterate Over Keys in a Map in C++ ?
  15. How to Iterate Over Values in a Map in C++ ?
  16. How to Iterate Over Entries (Key-Value Pairs) in a Map in C++ ?
  17. How to Get the Size (Number of Key-Value Pairs) of a Map in C++ ?
  18. How to Convert a Map to an Array of Keys in C++ ?
  19. How to Convert a Map to an Array of Values in C++ ?
  20. How to Convert a Map to an Array of Key-Value Pairs in C++ ?
  21. How to Merge Two Maps in C++ ?
  22. How to Copy a Map in C++ ?
  23. How to Check if Two Maps are Equal in C++ ?
  24. How to Sort a Map by Keys in C++ ?
  25. How to Sort a Map by Values in C++ ?
  26. How to Filter a Map Based on Keys in C++ ?
  27. How to Filter a Map Based on Values in C++ ?
  28. How to Reduce Values in a Map to a Single Value in C++ ?
  29. How to Convert an Array of Key-Value Pairs to a Map in C++ ?
  30. How to Convert a Map to a JSON String in C++ ?
  31. How to Convert a JSON String to a Map in C++ ?
  32. How to Swap Keys and Values in a Map in C++ ?
  33. How to Create a Map of Maps in C++ ?
  34. How to Iterate Over a Map of Maps in C++ ?