std::unordered_set
定义于头文件 <unordered_set> |
||
template< class Key, |
(1) | (since C++11) |
namespace pmr { template< |
(2) | (since C++17) |
std::unordered_set
是一个关联容器,包含一组类型为 Key
的唯一对象。搜索、插入和移除操作平均具有常数时间复杂度。
在内部,元素不以任何特定顺序排序,而是组织成桶(buckets)。元素被放入哪个桶完全取决于其值的哈希值。这允许快速访问单个元素,因为一旦计算出哈希值,它就指向元素所在的精确桶。
容器元素可能无法被修改(即使通过非常量迭代器),因为修改可能会更改元素的哈希值并破坏容器。
std::unordered_set
满足 Container、 AllocatorAwareContainer、 UnorderedAssociativeContainer 的要求。
|
(since C++26) |
目录 |
[编辑] 迭代器失效
操作 | 失效 |
---|---|
所有只读操作、 swap、 std::swap | 从不 |
clear、 rehash、 reserve、 operator= | 总是 |
insert、 emplace、 emplace_hint | 仅在引起重哈希时 |
erase | 仅对被移除的元素失效 |
[编辑] 注释
- swap 函数不会使容器内部的任何迭代器失效,但它们会使标记交换区域末尾的迭代器失效。
- 指向容器中存储的数据的引用和指针仅在移除该元素时失效,即使对应的迭代器失效。
- 在容器移动赋值之后,除非元素级移动赋值被不兼容的分配器强制执行,否则移动后容器的引用、指针和迭代器(past-the-end 迭代器除外)仍然有效,但引用现在位于 *this 中的元素。
[编辑] 模板参数
本节内容不完整 原因:添加模板参数的描述。 |
[编辑] 成员类型
类型 | 定义 |
key_type
|
Key |
value_type
|
Key |
size_type
|
无符号整数类型(通常为 std::size_t) |
difference_type
|
带符号整数类型(通常为 std::ptrdiff_t) |
hasher
|
Hash |
key_equal
|
KeyEqual |
allocator_type
|
Allocator |
reference
|
value_type& |
const_reference
|
const value_type& |
pointer
|
std::allocator_traits<Allocator>::pointer |
const_pointer
|
std::allocator_traits<Allocator>::const_pointer |
iterator
|
常量 LegacyForwardIterator 和 ConstexprIterator(since C++26) 指向 value_type 的迭代器 |
const_iterator
|
LegacyForwardIterator 和 ConstexprIterator(since C++26) 指向 const value_type 的迭代器 |
local_iterator
|
迭代器类型,其类别、值、差值、指针和 引用类型与 iterator 相同。此迭代器可用于遍历单个桶,但不能跨桶遍历 |
const_local_iterator
|
迭代器类型,其类别、值、差值、指针和 引用类型与 const_iterator 相同。此迭代器可用于遍历单个桶,但不能跨桶遍历 |
node_type (since C++17) |
一种 node handle 的特化,表示容器节点 |
insert_return_type (since C++17) |
描述插入 node_type 结果的类型,是template<class Iter, class NodeType> |
[编辑] 成员函数
构造 unordered_set (public member function) | |
析构 unordered_set (public member function) | |
为容器赋值 (public member function) | |
返回关联的分配器 (public member function) | |
迭代器 | |
返回指向开头的迭代器 (public member function) | |
返回指向末尾的迭代器 (public member function) | |
容量 | |
检查容器是否为空 (public member function) | |
返回元素数量 (public member function) | |
返回最大可能元素数量 (public member function) | |
修改器 | |
清除内容 (public member function) | |
插入元素 或节点(since C++17) (public member function) | |
(C++23) |
插入元素范围 (public member function) |
就地构造元素 (public member function) | |
使用提示就地构造元素 (public member function) | |
erases elements (public member function) | |
交换内容 (public member function) | |
(C++17) |
从容器中提取节点 (public member function) |
(C++17) |
从另一个容器拼接节点 (public member function) |
查找 | |
返回匹配特定键的元素数量 (public member function) | |
查找具有特定键的元素 (public member function) | |
(C++20) |
检查容器是否包含具有特定键的元素 (public member function) |
返回匹配特定键的元素范围 (public member function) | |
桶接口 | |
返回指向指定桶开头的迭代器 (public member function) | |
返回指向指定桶末尾的迭代器 (public member function) | |
返回桶的数量 (public member function) | |
返回最大桶数量 (public member function) | |
返回特定桶中的元素数量 (public member function) | |
返回特定键的桶 (public member function) | |
哈希策略 | |
返回每个桶的平均元素数 (public member function) | |
管理每个桶的最大平均元素数 (public member function) | |
预留至少指定数量的桶并重新生成哈希表 (public member function) | |
预留至少指定数量的元素的空间并重新生成哈希表 (public member function) | |
观察器 | |
返回用于哈希键的函数 (public member function) | |
返回用于比较键是否相等的函数 (public member function) |
[编辑] 非成员函数
(C++11)(C++11)(removed in C++20) |
比较 unordered_set 中的值 (function template) |
特化 std::swap 算法 (function template) | |
(C++20) |
移除所有满足特定条件的元素 (function template) |
推导指引 |
(since C++17) |
[编辑] 注释
成员类型 iterator
和 const_iterator
可能是相同类型的别名。这意味着使用这两种类型作为参数类型定义一对函数重载可能会违反 One Definition Rule。由于 iterator
可转换为 const_iterator
,因此使用 const_iterator
作为参数类型的单个函数即可替代。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的范围构造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::unordered_set |
[编辑] 示例
#include <iostream> #include <unordered_set> void print(const auto& set) { for (const auto& elem : set) std::cout << elem << ' '; std::cout << '\n'; } int main() { std::unordered_set<int> mySet{2, 7, 1, 8, 2, 8}; // creates a set of ints print(mySet); mySet.insert(5); // puts an element 5 in the set print(mySet); if (auto iter = mySet.find(5); iter != mySet.end()) mySet.erase(iter); // removes an element pointed to by iter print(mySet); mySet.erase(7); // removes an element 7 print(mySet); }
可能的输出
8 1 7 2 5 8 1 7 2 8 1 7 2 8 1 2
[编辑] 缺陷报告
以下行为更改的缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2050 | C++11 | reference 、 const_reference 、 pointer 的定义和 const_pointer 基于 allocator_type |
基于 value_type 和std::allocator_traits |
[编辑] 参见
(C++11) |
键的集合,按键哈希 (class template) |
唯一键的集合,按键排序 (class template) | |
(C++23) |
适配容器以提供唯一键的集合,按键排序 (class template) |