std::unordered_set
定义于头文件 <unordered_set> |
||
template< class Key, |
(1) | (C++11 起) |
namespace pmr { template< |
(2) | (C++17 起) |
std::unordered_set
是一个关联容器,它包含一组类型为 Key
的唯一对象。搜索、插入和删除具有平均常数时间复杂度。
在内部,元素不按任何特定顺序排序,而是组织成桶。元素被放入哪个桶完全取决于其值的哈希值。这允许快速访问单个元素,因为一旦计算出哈希值,它就指向元素被放入的确切桶。
容器元素不能被修改(即使通过非 const 迭代器),因为修改可能会改变元素的哈希值并破坏容器。
std::unordered_set
满足 Container、AllocatorAwareContainer、UnorderedAssociativeContainer 的要求。
|
(C++26 起) |
目录 |
[编辑] 迭代器失效
操作 | 失效 |
---|---|
所有只读操作,swap,std::swap | 从不 |
clear,rehash,reserve,operator= | 总是 |
insert,emplace,emplace_hint | 仅在导致重新哈希时 |
erase | 仅被擦除的元素 |
[编辑] 注意
- 交换函数不会使容器内的任何迭代器失效,但它们会使标记交换区域末尾的迭代器失效。
- 指向容器中存储数据的引用和指针仅在擦除该元素时才失效,即使相应的迭代器失效也是如此。
- 容器移动赋值后,除非由于不兼容的分配器强制进行元素级移动赋值,否则指向被移动容器的引用、指针和迭代器(除了尾后迭代器)保持有效,但它们现在指向 *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(C++26起) 到 value_type |
const_iterator
|
LegacyForwardIterator 和 ConstexprIterator(C++26起) 到 const value_type |
local_iterator
|
一种迭代器类型,其类别、值、差值、指针和 引用类型与 iterator 相同。此迭代器可用于迭代单个桶,但不能跨桶迭代 |
const_local_iterator
|
一种迭代器类型,其类别、值、差值、指针和 引用类型与 const_iterator 相同。此迭代器可用于迭代单个桶,但不能跨桶迭代 |
node_type (C++17 起) |
node handle 的特化,表示一个容器节点 |
insert_return_type (C++17起) |
描述插入 node_type 结果的类型,一个特化template<class Iter, class NodeType> |
[编辑] 成员函数
构造 unordered_set (公有成员函数) | |
析构 unordered_set (公有成员函数) | |
将值赋给容器 (公有成员函数) | |
返回关联的分配器 (公有成员函数) | |
迭代器 | |
返回指向起始的迭代器 (公有成员函数) | |
返回指向末尾的迭代器 (公有成员函数) | |
容量 | |
检查容器是否为空 (公有成员函数) | |
返回元素数量 (公有成员函数) | |
返回元素的最大可能数量 (公有成员函数) | |
修改器 | |
清除内容 (公有成员函数) | |
插入元素 或节点(C++17 起) (公有成员函数) | |
(C++23) |
插入元素范围 (公有成员函数) |
就地构造元素 (公有成员函数) | |
使用提示就地构造元素 (公有成员函数) | |
擦除元素 (公有成员函数) | |
交换内容 (公有成员函数) | |
(C++17) |
从容器中提取节点 (公有成员函数) |
(C++17) |
从另一个容器拼接节点 (公有成员函数) |
查找 | |
返回匹配特定键的元素数量 (公有成员函数) | |
查找具有特定键的元素 (公有成员函数) | |
(C++20) |
检查容器是否包含具有特定键的元素 (公有成员函数) |
返回与特定键匹配的元素范围 (公有成员函数) | |
桶接口 | |
返回指向指定桶开头的迭代器 (公有成员函数) | |
返回指向指定桶末尾的迭代器 (公有成员函数) | |
返回桶的数量 (公有成员函数) | |
返回最大桶数量 (公有成员函数) | |
返回特定桶中的元素数量 (公有成员函数) | |
返回特定键的桶 (公有成员函数) | |
哈希策略 | |
返回每个桶的平均元素数量 (公有成员函数) | |
管理每个桶的最大平均元素数量 (公有成员函数) | |
至少保留指定数量的桶并重新生成哈希表 (公有成员函数) | |
为至少指定数量的元素保留空间并重新生成哈希表 (公有成员函数) | |
观察器 | |
返回用于哈希键的函数 (公有成员函数) | |
返回用于比较键是否相等的函数 (公有成员函数) |
[编辑] 非成员函数
(C++11起)(C++11起)(C++20中移除) |
比较 unordered_set 中的值 (函数模板) |
特化 std::swap 算法 (函数模板) | |
(C++20) |
擦除所有满足特定标准的元素 (函数模板) |
推导指引 |
(C++17 起) |
[编辑] 注意
成员类型 iterator
和 const_iterator
可以是相同类型的别名。这意味着使用这两种类型作为参数类型定义一对函数重载可能违反 One Definition Rule。由于 iterator
可以转换为 const_iterator
,因此可以使用单个以 const_iterator
作为参数类型的函数。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2050 | C++11 | reference 、const_reference 、pointer 和 const_pointer 的定义基于 allocator_type |
基于 value_type 和std::allocator_traits |
[编辑] 另请参阅
(C++11) |
键的集合,按键哈希 (类模板) |
唯一键的集合,按键排序 (类模板) | |
(C++23) |
适配容器以提供唯一键的集合,按键排序 (类模板) |