flat_multimap_flat_multimap

flat_multimap_flat_multimap (std::flat_multimap::flat_multimap — 생성자)

std::flat_multimap 객체를 만드는 생성자들이에요. 기본 생성자부터 키/매핑값 컨테이너를 직접 넘기는 생성자, 정렬된 입력을 받는 std::sorted_equivalent_t 태그 생성자, 할당자(allocator) 생성자까지 다양한 방식으로 컨테이너를 초기화할 수 있어요.

출처: cppreference

본문

주요 시그니처는 다음과 같아요 (모두 since C++23).

flat_multimap();
template< class Allocator >
flat_multimap( const flat_multimap&, const Allocator& alloc );
template< class Allocator >
flat_multimap( flat_multimap&&, const Allocator& alloc );
flat_multimap( key_container_type key_cont, mapped_container_type mapped_cont,
               const key_compare& comp = key_compare() );
template< class Allocator >
flat_multimap( const key_container_type& key_cont,
               const mapped_container_type& mapped_cont,
               const Allocator& alloc );
template< class Allocator >
flat_multimap( const key_container_type& key_cont,
               const mapped_container_type& mapped_cont,
               const key_compare& comp, const Allocator& alloc );
flat_multimap( std::sorted_equivalent_t, key_container_type key_cont,
               mapped_container_type mapped_cont,
               const key_compare& comp = key_compare() );
template< class Allocator >
flat_multimap( std::sorted_equivalent_t, const key_container_type& key_cont,
               const mapped_container_type& mapped_cont, const Allocator& alloc );
template< class Allocator >
flat_multimap( std::sorted_equivalent_t, const key_container_type& key_cont,
               const mapped_container_type& mapped_cont,
               const key_compare& comp, const Allocator& alloc );
explicit flat_multimap( const key_compare& comp );
template< class Allocator >
flat_multimap( const key_compare& comp, const Allocator& alloc );
template< class Allocator >
explicit flat_multimap( const Allocator& alloc );

여러 오버로드가 있는데, 핵심은 다음과 같아요.

  • 기본 생성자: 빈 컨테이너를 만들어요. key_compare()를 쓰는 flat_multimap()으로 위임해요.
  • key_contmapped_cont를 받는 생성자: 주어진 두 컨테이너로 내부 저장소를 초기화한 뒤, 필요하면 정렬하고(중복 키가 있어도 됨) 조건을 검사해요. key_cont.size() == mapped_cont.size()가 아니면 동작이 정의되지 않아요.
  • std::sorted_equivalent_t 태그를 받는 생성자: 입력이 이미 정렬되어 있고 동등한 키들이 뭉쳐 있다고 가정하므로 정렬을 다시 하지 않아요. 이 경우에도 두 컨테이너 크기 조건과 정렬 조건을 만족하지 않으면 동작이 정의되지 않아요.
  • 할당자 오버로드: 지정한 할당자 alloc로 내부 컨테이너를 만들고 그 할당자를 사용해요.

보통 이런 생성자들은 중복 키를 허용하기 때문에 정렬 후 동등한 키를 병합하지 않아요(멀티맵이므로).

복잡도

  • 키 컨테이너를 받는 생성자: 정렬이 필요하면 N·log(N) (여기서 N은 원소 개수), 이미 정렬된 태그를 쓰거나 빈 기본 생성자면 상수/선형.

예외

할당 실패, 요소의 복사/이동 생성자가 던지는 예외 등이 전파될 수 있어요.

예제

#include <flat_map>
#include <vector>
int main()
{
    std::flat_multimap<int, char> fm;                 // 기본 생성
    std::vector<int> keys{2, 1, 2};
    std::vector<char> vals{'b', 'a', 'c'};
    std::flat_multimap<int, char> fm2(std::move(keys), std::move(vals)); // 컨테이너 이동
}

더 알아보기 (Learn more)

cppreference