std::set<Key,Compare,Allocator>:: set
| (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,
|
(4) | (constexpr since C++26) |
|
template
<
class
InputIt
>
set
(
InputIt first, InputIt last,
|
(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
(
)
,
|
(10) |
(C++11부터)
(C++26부터 constexpr) |
|
set
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(C++14부터)
(C++26부터 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(C++23부터)
(C++26부터 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(C++23부터)
(C++26부터 constexpr) |
다양한 데이터 소스로부터 새로운 컨테이너를 생성하며, 선택적으로 사용자가 제공한 할당자 alloc 또는 비교 함수 객체 comp 를 사용합니다.
[
first
,
last
)
.
[
first
,
last
)
가 유효한 범위가 아니라면, 동작은 정의되지 않습니다.
|
만약
alloc
이 제공되지 않으면, 할당자는
std::
allocator_traits
<
allocator_type
>
::
|
(C++11부터) |
|
클래스 템플릿 인수 추론
중에는 첫 번째 인수만이 컨테이너의
|
(C++23부터) |
|
클래스 템플릿 인수 추론
동안에는 첫 번째 인수만 컨테이너의
|
(C++23부터) |
내용매개변수
복잡도
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
(
)
로 정렬된 경우입니다.
예외
참고컨테이너 이동 생성(오버로드 (8,9)) 후에, 원래 비록 C++23까지 공식적으로 요구되지는 않지만, 일부 구현에서는 이미 이전 모드에서 템플릿 매개변수
예제
이 코드 실행
#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++ 표준에 소급 적용되었습니다.
참고 항목
| ||||||||||||||||||||||||||||||||||||||||||||||||||||