Implementing Multidimensional Map in C++ Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Multidimensional maps are used when we want to map a value to a combination of keys. The key can be of any data type, including those that are user-defined. Multidimensional maps are nested maps; that is, they map a key to another map, which itself stores combinations of key values with corresponding mapped values. Syntax: // Creating a two-dimensional map: map< key_1_type, map< key_2_type, value_type> > object; // Creating an N-dimensional map: map< key_1_type, map< key_2_type, ... map< key_N_type, value_type> > > object; Example 1: CPP14 // C++14 code to implement two-dimensional map #include <bits/stdc++.h> using namespace std; int main() { // Two-dimensional key map<int, map<int, int> > m; // For accessing outer map map<int, map<int, int> >::iterator itr; // For accessing inner map map<int, int>::iterator ptr; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { m[i][j] = i + j; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { // Accessing through array subscript cout << "First key is " << i << " And second key is " << j << " And value is " << m[i][j] << endl; } } cout << "\nNow accessing map though iterator \n\n"; for (itr = m.begin(); itr != m.end(); itr++) { for (ptr = itr->second.begin(); ptr != itr->second.end(); ptr++) { cout << "First key is " << itr->first << " And second key is " << ptr->first << " And value is " << ptr->second << endl; } } } Output: First key is 0 And second key is 0 And value is 0 First key is 0 And second key is 1 And value is 1 First key is 1 And second key is 0 And value is 1 First key is 1 And second key is 1 And value is 2 Now accessing map though iterator First key is 0 And second key is 0 And value is 0 First key is 0 And second key is 1 And value is 1 First key is 1 And second key is 0 And value is 1 First key is 1 And second key is 1 And value is 2 Example 2: CPP14 // C++14 code to implement two-dimensional map // and inserting value through insert() #include <bits/stdc++.h> using namespace std; int main() { // First key type is a string map<string, map<int, int> > m; map<string, map<int, int> >::iterator itr; map<int, int>::iterator ptr; m.insert(make_pair("Noob", map<int, int>())); m["Noob"].insert(make_pair(0, 5)); m.insert(make_pair("Geek", map<int, int>())); m["Geek"].insert(make_pair(1, 10)); m.insert(make_pair("Geek", map<int, int>())); m["Geek"].insert(make_pair(2, 20)); for (itr = m.begin(); itr != m.end(); itr++) { for (ptr = itr->second.begin(); ptr != itr->second.end(); ptr++) { cout << "First key is " << itr->first << " And second key is " << ptr->first << " And value is " << ptr->second << endl; } } } Output: First key is Geek And second key is 1 And value is 10 First key is Geek And second key is 2 And value is 20 First key is Noob And second key is 0 And value is 5 Comment More infoAdvertise with us Next Article Descending Order in Map and Multimap of C++ STL G gyanendra371 Follow Improve Article Tags : C++ cpp-map-functions Practice Tags : CPP Similar Reads Multidimensional Vectors in C++ A multidimensional vector is a vector of vectors that allows us to create 2D, 3D, or higher-dimensional vectors in a similar way as multidimensional arrays but unlike arrays, multidimensional vectors can grow and shrink dynamically.Let's take a quick look at a simple example of a multidimensional ar 7 min read How to print dimensions of multidimensional array in C++ Create a function that prints multidimensional array size by dimensions i.e.:Examples: Input : int a[2][3][4]; printDimensions(a); Output : 2x3x4 Input : int b[5][6]; printDimensions(a); Output : 5x6 To solve this problem we should use template function to figure out the size of current array. Then 2 min read Initialization of Multidimensional Arrays in C++ In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi 3 min read Descending Order in Map and Multimap of C++ STL We have discussed map in C++ STL and multimap in C++ STL. The default behavior of these data structures is to store elements in ascending order. How to store elements in reverse order or descending order when inserting in map and multimap? We can use the third parameter, that is std::greater along w 3 min read C++ Multidimensional Array A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the 8 min read multimap::emplace() in C++ STL The multimap::emplace() is a built-in function in C++ STL which inserts the key and its element in the multimap container. It effectively increases the container size by one as multimap is the container that stores multiple keys with same values. Syntax: multimap_name.emplace(key, element) Parameter 1 min read Like