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) |
適配容器以提供唯一鍵的集合,按鍵排序 (類模板) |