![]() |
VOOZH | about |
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"} }
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.
map < keytype, map <innerKeyType, valueType> > myMapThe below program demonstrates how we can create a map of maps in C++ STL.
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)