map
맵 기본 함수
- 기본형태
- map<key,value>: key와 value를 pair 형태로 선언합니다.
- iterator(반복자)
- begin(): beginning iterator를 반환
- end(): end iterator를 반환
- 추가 및 삭제
- insert( make_pair(key,value) ): 맵에 원소를 pair 형태로 추가
- erase(key): 맵에서 key(키값)에 해당하는 원소 삭제
- clear(): 맵의 원소들 모두 삭제
- 조회
- find(key): key(키값)에 해당하는 iterator를 반환
- count(key): key(키값)에 해당하는 원소들(value들)의 개수를 반환
- 기타
- empty(): 맵이 비어있으면 true 아니면 false를 반환
- size(): 맵 원소들의 수를 반환
...
map<string, int> m;
m["U"] = 1; m["D"] = -1; m["R"] = 1, m["L"] = -1;
...
if (c == 'U' || c == 'D') {
int next_y = cur_y + m[dir_c];
if ((next_y > max_coord) || (next_y < min_coord))
continue;
cur_y = next_y;
} else if (c == 'R' || c == 'L') {
int next_x = cur_x + m[dir_c];
if ((next_x > max_coord) || (next_x < min_coord))
continue;
cur_x = next_x;
}
....
- 나는 map 을 U(up), D(down), R(right), L(left)로 움직일 때 더해줘야할 인자 값을 저장하기 위해 사용했다.
- 접근은 key 값을 넘겨주면 해당 value 가 나와서 편리하다.
출처 : https://twpower.github.io/91-how-to-use-map-in-cpp