C++ map 라이브러리 코드 예제

map은 key와 value를 쌍으로 저장하는 컨테이너다.


#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
map<string, int> values;

values.insert(make_pair("Key1", 0));
values["Key2"] = 1;

cout << "Key1 : " << values.find("Key1")->second << endl;
cout << "Key2 : " << values["Key2"] << endl;
cout << "Map.empty() : " << values.empty() << endl;
cout << "Map.size() : " << values.size() << endl;

for (auto it = values.begin(); it != values.end(); ++it) {
cout << "Key: " << it->first << " Value: " << it->second << endl;
}
return 0;
}


insert와 대괄호 접근 방식으로 값을 추가할 수 있다.

키로 값을 찾고, 비어 있는지와 크기를 확인하고, 반복자로 전체 항목을 순회할 수 있다.

Comments

댓글을 불러오고 있습니다.