티스토리 뷰

 

#include <iostream>
#include <map>
using namespace std;

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

	dic["a"] = 1;
	dic["b"] = 2;
	dic["c"] = 3;
	dic["d"] = 4;
	int a = dic["e"];

	map<string, int>::iterator it;

	for (it= dic.begin(); it != dic.end(); it++) {
		cout << it->first << ' ' << it->second;
		cout << endl;
	}

	cout << a;
}

map자료형을 선언하고 key에 a b c d 그리고 값에 1 2 3 4를 넣었다.

 

map<string, int>::iterator it; 를 통해 반복자를 선언하여

반복자를 이용해서 반복문을 구현하였다.

 

또한 it->first를 통해 key에, it->second를 통해 값을 확인할 수 있었다.

 

추가로 key에 없는 값을 호출하면 자동으로 default 값 0 을 갖는 key가 생성된다.

댓글
반응형
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함