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) | (C++26 起为 constexpr) |
|
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
)
的内容构造容器。
|
若未提供
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·log(N),若
[
first
,
last
)
已按
value_comp
(
)
排序,则时间复杂度为线性。
6,7)
与
other
的大小成线性关系。
8,9)
常数复杂度。若提供
alloc
且
alloc
!
=
other.
get_allocator
(
)
,则为线性复杂度。
10,11)
N·log(N)
其中
N
为
init.
size
(
)
,通常情况下的复杂度,若
init
已按
value_comp
(
)
排序则为
N
的线性复杂度。
异常
对
注意容器移动构造(重载(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++标准。
参见
| ||||||||||||||||||||||||||||||||||||||||||||||||||||