[C++] multimap

초보개발·2021년 10월 22일
0

코딩테스트

목록 보기
4/30

multimap

map과 비슷하지만 multiset과 마찬가지로 key의 중복이 가능하다.
삽입될 때마다 정렬이 되므로 항상 정렬된 상태를 유지할 수 있다. (less key 가 기본값)
하지만 map과는 다르게 []를 이용하여 원소의 추가, 수정이 불가능하다.

#include <map> 
using namespace std;

int main(){
  // multimap<key의 자료형, value의 자료형> 변수명;
  multimap<string, int> mm;
  
  // multimap에 pair 객체로 삽입한다.
  mm.insert(pair<string, int>("Hello", 1));
  mm.insert(pair<string, int>("ABC", 2));
  mm.insert(pair<string, int>("ABC", 3));
  mm.insert(pair<string, int>("Hi", 4));
  
  // lower_bound
  auto start = mm.lower_bound("ABC");
  //upper_bound
  auto end = mm.upper_bound("ABC");
  
  cout<<start->first<<' '<<end->first<<endl; // ABC Hello
}

0개의 댓글