C++에서 다차원 지도 구현
다차원 지도 s는 값을 키 조합에 매핑하려고 할 때 사용됩니다. 키는 사용자 정의 데이터 유형을 포함하여 모든 데이터 유형이 될 수 있습니다. 다차원 지도는 중첩된 지도입니다. 즉, 키를 다른 맵에 매핑합니다. 이 맵은 키 값과 해당 매핑된 값의 조합을 자체적으로 저장합니다.
통사론:
// Creating a two-dimensional map: map object; // Creating an N-dimensional map: map>물체;
예시 1:
// C++14 code to implement two-dimensional map> > #include> using> namespace> std;> > int> main()> {> > > // Two-dimensional key> > map <> int> , map <> int> ,> int> >> 엠;> > > // For accessing outer map> > map <> int> , map <> int> ,> int> >>::반복자 itr;> > > // For accessing inner map> > map <> int> ,> int> >::반복자 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 < <> '
Now accessing map though iterator
'> ;> > > for> (itr = m.begin(); itr != m.end(); itr++) {> > > for> (ptr = itr->두 번째.시작(); ptr != itr->second.end(); ptr++) {> > cout < <> 'First key is '> < < ' And second key is ' < < ' And value is ' } } }> |
산출:
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
예시 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>> 엠; mapint, int>>::iterator itr; 지도 |
산출:
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