set

set (std::set — 정렬 셋)

std::set은 중복이 없는 Key 타입 객체들의 정렬된 집합을 담는 연관 컨테이너예요. 정렬은 키 비교 함수 Compare로 수행돼요. 검색·제거·삽입 연산은 로그 시간 복잡도를 가져요.

출처: cppreference

본문

std::set은 중복이 없는 Key 타입 객체들의 정렬된 집합을 담는 연관 컨테이너예요. 정렬은 키 비교 함수 Compare로 수행돼요. 검색·제거·삽입 연산은 로그(logarithmic) 복잡도를 가져요. 셋은 보통 Red–black tree로 구현돼요.

표준 라이브러리가 Compare 요구 사항을 쓰는 곳마다, 동등성은 동치 관계(equivalence relation)로 결정돼요. 간단히 말해 두 객체 ab는 서로가 상대보다 작다고 비교되지 않을 때(!comp(a, b) && !comp(b, a)) 동등한 것으로 봐요.

std::setContainer, AllocatorAwareContainer, AssociativeContainer, ReversibleContainer의 요구 사항을 만족해요.

템플릿 매개변수

  • Key: 저장된 원소(키)의 타입.
  • Compare: 엄격한 약순서(strict weak ordering)를 제공하는 Compare 타입. 기본값은 std::less<Key>.
  • Allocator: 할당자. 기본값은 std::allocator<Key>.

멤버 타입

  • key_type = Key, value_type = Key
  • key_compare = Compare, value_compare
  • size_type, difference_type, reference, const_reference, pointer, const_pointer
  • iterator, const_iterator, reverse_iterator, const_reverse_iterator
  • node_type, insert_return_type

멤버 함수

  • 생성자/파괴자, operator=, get_allocator
  • 이터레이터: begin, end, rbegin, rend
  • 용량: empty, size, max_size
  • 수정자: clear, insert, insert_range, emplace, emplace_hint, erase, erase_if, swap, extract, merge
  • 탐색: count, find, contains, equal_range, lower_bound, upper_bound
  • 관찰자: key_comp, value_comp
  • 비멤버 함수: operator==, operator<=>, std::erase_if, std::swap

더 알아보기 (Learn more)

cppreference