std::set<Key,Compare,Allocator>::set
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
| (1) | ||
set(); |
(C++11 前) | |
set() : set(Compare()) {} |
(C++11 起) | |
explicit set( const Compare& comp, {{#pad:|12}} const Allocator& alloc = Allocator() ); |
(2) | |
explicit set( const Allocator& alloc ); |
(3) | (C++11 起) |
template< class InputIt > set( InputIt first, InputIt last, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(4) | |
template< class InputIt > set( InputIt first, InputIt last, {{#pad:|3}} const Allocator& alloc ) : set(first, last, Compare(), alloc) {} |
(5) | (C++14 起) |
set( const set& other ); |
(6) | |
set( const set& other, const Allocator& alloc ); |
(7) | (C++11 起) |
set( set&& other ); |
(8) | (C++11 起) |
set( set&& other, const Allocator& alloc ); |
(9) | (C++11 起) |
set( std::initializer_list<value_type> init, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(10) | (C++11 起) |
set( std::initializer_list<value_type> init, {{#pad:|3}} const Allocator& alloc ) : set(init, Compare(), alloc) {} |
(11) | (C++14 起) |
template< container-compatible-range<value_type> R > set( std::from_range_t, R&& rg, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(12) | (C++23 起) |
template< container-compatible-range<value_type> R > set( std::from_range_t, R&& rg, {{#pad:|3}} const Allocator& alloc ) : set(std::from_range, std::forward<R>(rg), Compare(), alloc) {} |
(13) | (C++23 起) |
从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 或比较函数对象 comp。
1-3) 构造空容器。
6,7) 复制构造函数。以
other 的内容副本构造容器。
|
若不提供 |
(C++11 起) |
|
在用于类模板实参推导时,仅从首个实参推导模板形参 |
(C++23 起) |
8,9) 移动构造函数。以
other 的内容用移动语义构造容器。若不提供 alloc,则从属于 other 的分配器移动构造其分配器。
|
在用于类模板实参推导时,仅从首个实参推导模板形参 |
(C++23 起) |
参数
| alloc | - | 用于此容器所有内存分配的分配器 |
| comp | - | 用于进行所有键比较的比较函数对象 |
| first, last | - | 要复制的源元素范围的迭代器对 |
| other | - | 要用作源以初始化容器元素的另一容器 |
| init | - | 用以初始化容器元素的 initializer_list |
| rg | - | 容器兼容范围,即其元素可以转换为 value_type 的 input_range
|
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-Compare 必须满足比较 (Compare) 。
| ||
-Allocator 必须满足分配器 (Allocator) 。
| ||
复杂度
1-3) 常数。
4,5) N·log(N),其中 N 通常为
std::distance(first, last),若 [first, last) 已按照 value_comp() 排序则与 N 成线性。6,7) 与
other 的大小成线性。8,9) 常数。若给定
alloc 且 alloc != other.get_allocator() 则为线性。10,11) N·log(N),其中 N 通常为
init.size(),若 init 已按照 value_comp() 排序则与 N 成线性。12,13) N·log(N),其中 N 通常为
ranges::distance(rg),若 rg 已按照 value_comp() 排序则与 N 成线性。异常
对 Allocator::allocate 的调用可能抛出。
注解
在容器移动构造(重载 (8,9))后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但将指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 问题 2321 正在考虑更严格的保证。
尽管在 C++23 前未正式要求,一些实现已经在较早的模式中将 Allocator 放入非推导语境。
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 按范围构造和插入; 重载 (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> z = {{2, 5}, {3, 4}, {1, 1}};
z.insert({1, -1}); // 因为 (1,-1) 的长度等于 (1,1) 而可能失败
println("7) f: ", f);
// (12) 范围构造函数
const auto w = {"Eurybia", "Theia", "Rhea", "Aura", "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++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2076 | C++11 | 重载 (4) 条件性要求 Key 可复制插入 (CopyInsertable) 到 *this
|
不要求 |
| LWG 2193 | C++11 | 默认构造函数为 explicit | 使之为非 explicit |
参阅
| 将值赋给容器 (公开成员函数) |