Implementarea hărții multidimensionale în C++

Multidimensional Hartă s sunt folosite atunci când dorim să mapam o valoare la o combinație de taste. Cheia poate fi de orice tip de date, inclusiv cele care sunt definite de utilizator. Hărțile multidimensionale sunt hărți imbricate; adică mapează o cheie la o altă hartă, care stochează ea însăși combinații de valori cheie cu valorile mapate corespunzătoare.

Sintaxă:

 // Creating a two-dimensional map: map object; // Creating an N-dimensional map: map>obiect;>>>   

Exemplul 1:




// C++14 code to implement two-dimensional map> > #include> using> namespace> std;> > int> main()> {> > > // Two-dimensional key> > map <> int> , map <> int> ,> int> >> m;>>> > // For accessing inner map> > map <> int> ,> int> >::iterator ptr;>>> < < i> > < <> ' And second key is '> < < j> > < <> ' And value is '> < < m[i][j] < < endl;> > }> > }> > > cout < <> ' Now accessing map though iterator '> ;> > > for> (itr = m.begin(); itr != m.end(); itr++) {> > > for> (ptr = itr->second.begin(); ptr != itr->second.end(); ptr++) {>> > cout < <> 'First key is '> < < ' And second key is ' < < ' And value is ' } } }>

Ieșire:

 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 

Exemplul 2:




// C++14 code to implement two-dimensional map> // and inserting value through insert()> > #include> using> namespace> std;> > int> main()> {> > > // First key type is a string> > mapint, int>> m; mapint, int>>::iterator itr; Hartă ::iterator ptr; m.insert(make_pair('Noob', map ())); m['Noob'].insert(make_pair(0, 5)); m.insert(make_pair('Geek', map ())); m['Geek'].insert(make_pair(1, 10)); m.insert(make_pair('Geek', map ())); m['Geek'].insert(make_pair(2, 20)); pentru (itr = m.begin(); itr != m.end(); itr++) { for (ptr = itr->second.begin(); ptr != itr->second.end(); ptr++) { cout < < 'First key is ' < < ' And second key is ' < < ' And value is ' } } }>

Ieșire:

 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