Implementing Multidimensional Map in C++
Multidimensional map are used when we want to map value to combination of key, here key can be of any data type or user defined data type.Multidimensional map are nested map that stores elements formed by the combination of a key value and a mapped value.
Syntax:
// for creating Two dimensional map is map< key_1_type, map< key_2_type, value_type> > object; // for creating N-dimensional map is map< key_1_type, map< key_2_type, ... map< key_N_type, value_type> > > object;
Example 1:
// 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; } } } |
chevron_right
filter_none
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:
// 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; } } } |
chevron_right
filter_none
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
Recommended Posts:
- Implementing ANDNOT Gate using Adaline Network
- Left-Right traversal of all the levels of N-ary tree
- C/C++ program to print Hello World without using main() and semicolon
- Find the count of unvisited indices in an infinite array
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Find the sum of remaining sticks after each iterations
- C++program to delete the content of a Binary File
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Public vs Protected in C++ with Examples
- C++ program to modify the content of a Binary File
- How to Iterate through a String word by word in C++
- Difference between Public and Private in C++ with Example
- C++ Program to Illustrate Trigonometric functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


