VOOZH about

URL: https://www.geeksforgeeks.org/cpp/implementing-multidimensional-map-in-c/

⇱ Implementing Multidimensional Map in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing Multidimensional Map in C++

Last Updated : 12 Jul, 2025
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:
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:
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
Article Tags: