flat_set_flat_set

flat_set_flat_set (std::flat_set::flat_set — 생성자)

std::flat_set 객체를 만드는 생성자들이에요. 기본 생성자부터 내부 컨테이너를 직접 넘기는 생성자, 정렬+중복 없음 입력을 받는 std::sorted_unique_t 태그 생성자, 할당자(allocator) 생성자까지 다양한 방식으로 초기화할 수 있어요.

출처: cppreference

본문

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

flat_set();
template< class Allocator >
flat_set( const flat_set& other, const Allocator& alloc );
template< class Allocator >
flat_set( flat_set&& other, const Allocator& alloc );
explicit flat_set( container_type cont,
                   const key_compare& comp = key_compare() );
template< class Allocator >
flat_set( const container_type& cont, const Allocator& alloc );
template< class Allocator >
flat_set( const container_type& cont, const key_compare& comp,
          const Allocator& alloc );
flat_set( std::sorted_unique_t s, container_type cont,
          const key_compare& comp = key_compare() );

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

  • 기본 생성자: 빈 컨테이너를 만들어요. key_compare()를 쓰는 flat_set(key_compare())으로 위임해요.
  • 복사/이동 + 할당자: 같은 내용을 주어진 할당자 alloc로 만든 컨테이너에 복사·이동해요.
  • cont를 받는 생성자: 주어진 내부 컨테이너로 저장소를 초기화하고, 필요하면 정렬하고 중복을 제거해요.
  • std::sorted_unique_t 태그를 받는 생성자: 입력이 이미 정렬되어 있고 중복이 없다고 가정하므로 정렬·중복 제거를 다시 하지 않아요. 조건을 만족하지 않으면 동작이 정의되지 않아요.
  • 할당자 오버로드: 지정한 할당자 alloc로 내부 컨테이너를 만들고 그 할당자를 사용해요.

복잡도

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

예외

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

예제

#include <flat_set>
#include <vector>
int main()
{
    std::flat_set<int> fs;                // 기본 생성
    std::vector<int> data{2, 1, 3, 1};
    std::flat_set<int> fs2(std::move(data));  // 정렬 + 중복 제거 후 이동
}

더 알아보기 (Learn more)

cppreference