How to Store Maps in a Map in C++? Last Updated : 29 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a map is a container that stores elements as a key value and a mapped value. A set is a container that stores unique elements in a particular order. In this article, we will learn how to create a map of sets in C++ STL. Example: myMap: { {1, {{1, "Apple"}, {2, "Banana"}}}, {2, {{3, "Cherry"}, {4, "Date"}}} }Store std::maps in Another std::map in C++To store maps in a map in C++, we can simply define the value type of the outer map as another map. This allows us to store key-value pairs where the values themselves are maps. Syntaxmap <outerKeyType, map<innerKeyType, innerValueType>> mapNameC++ Program to Store Maps in a Map C++ // C++ program that demonstrates how to store maps in a map #include <iostream> #include <map> using namespace std; int main() { // Initialize a map of maps map<int, map<int, string> > myMap = { { 1, { { 1, "Apple" }, { 2, "Banana" } } }, { 2, { { 3, "Cherry" }, { 4, "Date" } } } }; // Print the map of maps for (const auto& pair1 : myMap) { cout << pair1.first << ":\n"; for (const auto& pair2 : pair1.second) { cout << " " << pair2.first << ": " << pair2.second << "\n"; } } return 0; } Output1: 1: Apple 2: Banana 2: 3: Cherry 4: Date Time Complexity: O(N log N), where N is the number of inner maps. Space Complexity: O(N * M), M is the average size of the inner maps. Comment More infoAdvertise with us Next Article How to Find the Minimum Key in a Map in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S 2 min read How to Insert Pairs into a Map in C++? In C++, maps are STL containers that store key-value pairs and maintain the element's order according to their keys. Pairs also store the data as key values. In this article, we will discuss how to insert a pair into a map in C++. For Example, Input: map<int, string> mp = {(1, "geek"), (2,"for 2 min read How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read How to Find the Minimum Key in a Map in C++? In C++, a map is an associative container that stores key-value pairs. In this article, we will learn how to find the minimum key in a map in C++. Example: Input: myMap= {{10,"apple"},{5,"banana"},{20,"cherry"}};Output:Minimum key: 5Find the Smallest Key in a Map in C++There is no direct method avai 2 min read How to Find the Size of a Map in Bytes in C++? In C++, maps are associative containers that store a key value and a mapped value for each element and no two mapped values can have the same key values. In this article, we will explore how to find the size of the map in bytes in C++. Example Input: myMap = {{âappleâ, 1}, {âbananaâ, 2}, {âcherryâ, 2 min read How to insert data in the map of strings? Maps are associative containers that store elements in a specific order. It stores elements in a combination of key values and mapped values. Syntax: map<data type of key, data type of value> M To use the above syntax for the map in C++, it is important to include the below header file:Header 3 min read Like