std::multiset<Key,Compare,Allocator>::multiset
来自cppreference.com
| (1) | (C++11 前) | |
| (C++11 起) | ||
| |
(2) | |
| |
(3) | (C++11 起) |
| (4) | ||
| |
(5) | (C++14 起) |
| |
(6) | |
| |
(7) | (C++11 起) |
| (8) | (C++11 起) | |
| |
(9) | (C++11 起) |
| |
(10) | (C++11 起) |
| |
(11) | (C++14 起) |
| (12) | (C++23 起) | |
| |
(13) | (C++23 起) |
从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 或比较函数对象 comp。
1-3) 构造空容器。
4,5) 以范围
[first, last) 的内容构造容器。6,7) 复制构造函数。以
other 的内容副本构造容器。
|
若不提供 std::allocator_traits<allocator_type>::
select_on_container_copy_construction(other.get_allocator())
|
(C++11 起) |
|
在用于类模板实参推导时,仅从首个实参推导模板形参 |
(C++23 起) |
8,9) 移动构造函数。以
other 的内容用移动语义构造容器。若不提供 alloc,则从属于 other 的分配器移动构造其分配器。
|
在用于类模板实参推导时,仅从首个实参推导模板形参 |
(C++23 起) |
12,13) 以
rg 的内容构造容器。参数
| 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) |
示例
Run this code
#include <iostream>
#include <set>
#include <string_view>
template <typename T>
void println(const std::string_view name, const std::multiset<T>& ms)
{
std::cout << name << ": ";
for (const auto& element : ms)
std::cout << element << ' ';
std::cout << '\n';
}
int main()
{
// (1) 默认构造函数
std::multiset<int> a;
a.insert(4);
a.insert(3);
a.insert(2);
a.insert(1);
println("a", a);
// (4) 范围构造函数
std::multiset<int> b(a.begin(), a.find(3));
println("b", b);
// (6) 复制构造函数
std::multiset<int> c(a);
println("c", c);
// (8) 移动构造函数
std::multiset<int> d(std::move(a));
println("d", d);
// (10) 初始化式列表构造函数
std::multiset<int> e{3, 2, 1, 2, 4, 7, 3};
println("e", e);
// (12) 范围构造函数
const auto w = {"α", "β", "γ", "δ", "δ", "γ", "β", "α"};
#if __cpp_lib_containers_ranges
std::multiset<std::string> f(std::from_range, w); // 重载 (12)
#else
std::multiset<std::string> f(w.begin(), w.end()); // 回退到 (4)
#endif
println("f", f);
}
输出:
a: 1 2 3 4
b: 1 2
c: 1 2 3 4
d: 1 2 3 4
e: 1 2 2 3 3 4 7
f: α α β β γ γ δ δ
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2076 | C++11 | 重载 (4) 条件性要求 Key 可复制插入 (CopyInsertable) 到 *this
|
不要求 |
| LWG 2193 | C++11 | 默认构造函数为 explicit | 使之为非 explicit |
参阅
| 将值赋给容器 (公开成员函数) |