파이썬 사전은 해시맵입니다. C++의 맵 데이터 구조를 사용하여 파이썬 사전의 동작을 모방할 수 있습니다. 다음과 같이 C++에서 맵을 사용할 수 있습니다.
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Initializer_list constructor */
map<char, int> m1 = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5}
};
cout << "Map contains following elements" << endl;
for (auto it = m1.begin(); it != m1.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
} 이것은 출력을 줄 것입니다
The map contains following elements a = 1 b = 2 c = 3 d = 4 e = 5
이 맵은 python dict:
와 동일합니다.m1 = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5
}