Skip to content

Std::map

Observers

key_comp
Return key comparison object.
value_comp
Return value comparison object.

Lookup methods

count
returns the number of elements matching specific key.
find
finds element with specific key.
equal_range
returns range of elements matching a specific key.
특정 key 값의 범위를 iterator의 쌍으로 반환.
lower_bound
returns an iterator to the first element not less than the given value.
지정한 key의 요소를 가지고 있다면 해당 위치의 반복자를 반환.
upper_bound
returns an iterator to the first element greater than a certain value.
지정한 key 보다 1단위 만큼 큰 첫 번째 반복자를 반환.

Insert vs []

std::map의 자료입력시 insert()와 array element access([])의 차이점은 아래와 같다.

  • insert: 중복 키 일 경우 값 무시.
  • [key]: key에 해당하는 값이 있을 경우 값을 변경. key에 값이 없을 경우 추가.

Example

중복 요소의 반복자 획득

equal_range를 사용한 예제는 아래와 같다.

multimap<int, string> m;
// ...
pair< multimap<int, string>::iterator, multimap<int, string>::iterator > p = m.equal_range(2);
for(i=p.first; i!=p.second; i++) { // Key 값이 2인 모든 원소 조회
    cout << i->second << endl;
}

또는 lower_boundupper_bound를 사용할 수 있다.

for(i=m.lower_bound(2); i!=m.upper_bound(2); i++) { // Key 값이 2인 모든 원소 조회
    cout << i->second << endl;
}

특정 Key값의 내용을 전부 삭제

equal_range를 사용한 예제는 아래와 같다. multimap m; // ... m.erase( m.find(2), m.find(3) ); // 2번 원소만 삭제됨 (2이상 3 미만)

</syntaxhighlight>

Favorite site