Los mapas multidimensionales se utilizan cuando queremos asignar un valor a una combinación de claves. La clave puede ser de cualquier tipo de datos, incluidos los definidos por el usuario. Los mapas multidimensionales son mapas anidados; es decir, asignan una clave a otro mapa, que a su vez almacena combinaciones de valores clave con los valores asignados correspondientes.
Sintaxis:
// 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;
Ejemplo 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; } } }
Producción:
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
Ejemplo 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; } } }
Producción:
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
Publicación traducida automáticamente
Artículo escrito por gyanendra371 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA