VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-create-a-map-of-maps-in-cpp/

⇱ How to Create a Map of Maps in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create a Map of Maps in C++?

Last Updated : 23 Jul, 2025

In C++, maps are associative containers provided by the STL library of C++, that allow the users to store data in key-value pairs where the keys must be unique. In this article, we will learn how to create a map of maps in C++.

For Example,

Input:
myMap1={1, "C++"};
myMap2={1, "Java"};
myMap3={2, "Python"};

Output:
Map Elements: { {1: {1, "C++"} },
 {2: {1, "Java"} },
 {3: {2, "Python"} }

Map of Maps in C++

To create a std::map of maps first, we need to declare a map where the key is of the desired type and the value is of the type map. We can do that by passing the value template parameter as a map.

Syntax to Declare Map of Maps

map < keytype, map <innerKeyType, valueType> > myMap

C++ Program to Create a Map of Maps

The below program demonstrates how we can create a map of maps in C++ STL.


Output
Map of maps:
1, 1 => one
1, 2 => two
2, 1 => two
2, 2 => four

Time Complexity: O(N * M log(N*M) ), here N is the number of maps in the map and M is the average size of the inner maps
Auxiliary Space: O(N * M)

Comment
Article Tags: