Namespaces
Variants

std::set<Key,Compare,Allocator>:: set

From cppreference.net

(1)
set ( ) ;
(C++11 이전)
set ( ) : set ( Compare ( ) ) { }
(C++11 이후)
(C++26 이후 constexpr)
explicit set ( const Compare & comp,
const Allocator & alloc = Allocator ( ) ) ;
(2) (C++26부터 constexpr)
explicit set ( const Allocator & alloc ) ;
(3) (C++11부터)
(C++26부터 constexpr)
template < class InputIt >

set ( InputIt first, InputIt last,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr since C++26)
template < class InputIt >

set ( InputIt first, InputIt last,
const Allocator & alloc )

: set ( first, last, Compare ( ) , alloc ) { }
(5) (C++14부터)
(C++26부터 constexpr)
set ( const set & other ) ;
(6) (C++26부터 constexpr)
set ( const set & other, const Allocator & alloc ) ;
(7) (C++11부터)
(C++26부터 constexpr)
set ( set && other ) ;
(8) (C++11부터)
(C++26부터 constexpr)
set ( set && other, const Allocator & alloc ) ;
(9) (C++11부터)
(C++26부터 constexpr)
set ( std:: initializer_list < value_type > init,

const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(10) (C++11부터)
(C++26부터 constexpr)
set ( std:: initializer_list < value_type > init,

const Allocator & alloc )

: set ( init, Compare ( ) , alloc ) { }
(11) (C++14부터)
(C++26부터 constexpr)
template < container-compatible-range < value_type > R >

set ( std:: from_range_t , R && rg,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(12) (C++23부터)
(C++26부터 constexpr)
template < container-compatible-range < value_type > R >

set ( std:: from_range_t , R && rg,
const Allocator & alloc )

: set ( std:: from_range , std:: forward < R > ( rg ) , Compare ( ) , alloc ) { }
(13) (C++23부터)
(C++26부터 constexpr)

다양한 데이터 소스로부터 새로운 컨테이너를 생성하며, 선택적으로 사용자가 제공한 할당자 alloc 또는 비교 함수 객체 comp 를 사용합니다.

1-3) 빈 컨테이너를 생성합니다.
4,5) 범위의 내용으로 컨테이너를 생성합니다 [ first , last ) .
만약 [ first , last ) 가 유효한 범위가 아니라면, 동작은 정의되지 않습니다.
6,7) other 의 내용을 복사하여 컨테이너를 생성합니다.

만약 alloc 이 제공되지 않으면, 할당자는 std:: allocator_traits < allocator_type > ::
select_on_container_copy_construction ( other. get_allocator ( ) )
을 호출하여 얻습니다.

(C++11부터)

클래스 템플릿 인수 추론 중에는 첫 번째 인수만이 컨테이너의 Allocator 템플릿 매개변수 추론에 기여합니다.

(C++23부터)
8,9) 이동 의미론을 사용하여 other 의 내용으로 컨테이너를 생성합니다. alloc 이 제공되지 않으면, other 에 속한 할당자로부터 이동 생성으로 할당자를 획득합니다.

클래스 템플릿 인수 추론 동안에는 첫 번째 인수만 컨테이너의 Allocator 템플릿 매개변수 추론에 기여합니다.

(C++23부터)
10,11) 초기화자 목록 init 의 내용으로 컨테이너를 생성합니다.
12,13) rg 의 내용으로 컨테이너를 생성합니다.

목차

매개변수

alloc - 이 컨테이너의 모든 메모리 할당에 사용할 allocator
comp - 키의 모든 비교에 사용할 비교 함수 객체
first, last - 복사할 요소들의 소스 range 를 정의하는 반복자 쌍
other - 컨테이너의 요소를 초기화하는 데 사용할 소스로 사용할 다른 컨테이너
init - 컨테이너의 요소를 초기화하는 데 사용할 initializer list
rg - container compatible range , 즉 요소들이 value_type 으로 변환 가능한 input_range
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함
-
Compare Compare 요구사항을 충족해야 함
-
Allocator Allocator 요구사항을 충족해야 함

복잡도

1-3) 상수.
4,5) N·log(N) 여기서 N std:: distance ( first, last ) 입니다. 일반적으로 N 에 선형적이며, [ first , last ) 가 이미 value_comp ( ) 에 의해 정렬된 경우 N 에 선형입니다.
6,7) other 의 크기에 선형적입니다.
8,9) 상수. 만약 alloc 이 주어지고 alloc ! = other. get_allocator ( ) 인 경우, 선형 시간.
10,11) N·log(N) 여기서 N init. size ( ) 입니다. 일반적으로 N 에 선형적이며, init 가 이미 value_comp ( ) 로 정렬된 경우에는 선형 시간입니다.
12,13) N·log(N) 여기서 N ranges:: distance ( rg ) 입니다. 일반적으로 N 에 선형적이며, rg 가 이미 value_comp ( ) 로 정렬된 경우입니다.

예외

Allocator::allocate 에 대한 호출은 예외를 발생시킬 수 있습니다.

참고 사항

컨테이너 이동 생성 후 (오버로드 ( 8,9 ) ), other 에 대한 참조, 포인터 및 반복자(끝 반복자 제외)는 유효하게 유지되지만, 이제는 * this 에 있는 요소들을 참조합니다. 현재 표준은 [container.reqmts]/67 의 포괄적 명시를 통해 이 보장을 제공하며, LWG issue 2321 를 통해 더 직접적인 보장이 검토 중입니다.

범위 내에서 비교 동등한 키를 가진 여러 요소가 있는 경우, 어떤 요소가 삽입될지 명시되지 않습니다 (보류 중인 LWG2844 ).

C++23까지 공식적으로 요구되지는 않지만, 일부 구현에서는 이미 이전 모드에서 템플릿 매개변수 Allocator 비추론 컨텍스트 에 포함시켰습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware 생성 및 삽입; 오버로드 ( 12,13 )

예제

#include <cmath>
#include <iostream>
#include <set>
#include <string>
struct Point { double x, y; };
struct PointCmp
{
    bool operator()(const Point& lhs, const Point& rhs) const
    {
        return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y);
    }
};
std::ostream& operator<<(std::ostream& os, Point pt)
{
    return os << '(' << pt.x << ',' << pt.x << ')';
}
void println(auto rem, const auto& seq)
{
    std::cout << rem << '{';
    for (auto n{seq.size()}; const auto& elm : seq)
        std::cout << elm << (--n ? ", " : "");
    std::cout << "}\n";
}
int main()
{
    // (1) 기본 생성자
    std::set<std::string> a;
    a.insert("horse");
    a.insert("cat");
    a.insert("dog");
    println("1) a: ", a);
    // (4) 범위 생성자
    std::set<std::string> b(a.find("dog"), a.end());
    println("2) b: ", b);
    // (6) 복사 생성자
    std::set<std::string> c(a);
    c.insert("another horse");
    println("3) c: ", c);
    // (8) 이동 생성자
    std::set<std::string> d(std::move(a));
    println("4) d: ", d);
    println("5) a: ", a);
    // (10) 초기화 리스트 생성자
    std::set<std::string> e{"one", "two", "three", "five", "eight"};
    println("6) e: ", e);
    // 사용자 정의 비교 함수
    std::set<Point, PointCmp> f = {{2, 5}, {3, 4}, {1, 1}};
    f.insert({1, -1}); // (1,-1)의 크기가 (1,1)과 같기 때문에 실패함
    println("7) f: ", f);
    // (12) 범위 생성자
    const auto w = {"Eurybia", "Theia", "Rhea", "Aura", "Mnemosyne", "Mnemosyne"};
#if __cpp_lib_containers_ranges
    std::set<std::string> g(std::from_range, w); // 오버로드 (12)
#else
    std::set<std::string> g(w.begin(), w.end()); // (4)로 폴백
#endif
    println("8) g: ", g);
}

가능한 출력:

1) a: {cat, dog, horse}
2) b: {dog, horse}
3) c: {another horse, cat, dog, horse}
4) d: {cat, dog, horse}
5) a: {}
6) e: {eight, five, one, three, two}
7) f: {(1,1), (3,3), (2,2)}
8) g: {Aura, Eurybia, Mnemosyne, Rhea, Theia}

결함 보고서

다음 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2076 C++11 오버로드 ( 4 ) 가 조건부로 요구했음 Key CopyInsertable 여야 * this 요구되지 않음
LWG 2193 C++11 기본 생성자가 explicit였음 non-explicit로 변경됨

참고 항목

컨테이너에 값을 할당합니다
(public member function)