std::unordered_map
在標頭檔案 <unordered_map> 中定義 |
||
template< class Key, |
(1) | (C++11 起) |
namespace pmr { template< |
(2) | (C++17 起) |
std::unordered_map
是一個關聯容器,包含鍵值對,且鍵是唯一的。元素的查詢、插入和刪除具有平均常數時間複雜度。
在內部,元素不按任何特定順序排序,而是組織成桶。元素被放入哪個桶完全取決於其鍵的雜湊值。具有相同雜湊碼的鍵出現在同一個桶中。這允許快速訪問單個元素,因為一旦計算出雜湊值,它就指向元素所在的精確桶。
如果對映的鍵相等謂詞在傳入兩個鍵時返回 true,則兩個鍵被認為是等價的。如果兩個鍵等價,則雜湊函式必須為兩個鍵返回相同的值。
std::unordered_map
滿足 Container、AllocatorAwareContainer、UnorderedAssociativeContainer 的要求。
|
(C++26 起) |
目錄 |
[編輯] 迭代器失效
操作 | 失效 |
---|---|
所有隻讀操作,swap,std::swap | 永不 |
clear,rehash,reserve,operator= | 總是 |
insert,emplace,emplace_hint,operator[] | 僅當導致重新雜湊時 |
erase | 僅對被擦除的元素 |
[編輯] 注
- 交換函式不會使容器內的任何迭代器失效,但會使標記交換區域末尾的迭代器失效。
- 指向容器中儲存的鍵或資料的引用和指標僅在刪除該元素時失效,即使相應的迭代器失效也是如此。
[編輯] 模板引數
本節不完整 理由:新增模板引數的描述。 |
[編輯] 成員型別
型別 | 定義 |
key_type
|
Key |
mapped_type
|
T |
value_type
|
std::pair<const Key, T> |
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 起) |
節點控制代碼的特化,表示容器節點 |
insert_return_type (C++17起) |
描述插入 node_type 結果的型別,一個特化template<class Iter, class NodeType> |
[編輯] 成員函式
構造 unordered_map (公共成員函式) | |
銷燬 unordered_map (公共成員函式) | |
將值賦給容器 (公共成員函式) | |
返回關聯的分配器 (公共成員函式) | |
迭代器 | |
返回指向起始的迭代器 (公共成員函式) | |
返回指向末尾的迭代器 (公共成員函式) | |
容量 | |
檢查容器是否為空 (公共成員函式) | |
返回元素數量 (公共成員函式) | |
返回元素的最大可能數量 (公共成員函式) | |
修改器 | |
清除內容 (公共成員函式) | |
插入元素 或節點(C++17 起) (公共成員函式) | |
(C++23) |
插入元素範圍 (公共成員函式) |
(C++17) |
插入元素或如果鍵已存在則賦值給當前元素 (公共成員函式) |
就地構造元素 (公共成員函式) | |
使用提示就地構造元素 (公共成員函式) | |
(C++17) |
如果鍵不存在則原地插入,如果鍵存在則不執行任何操作 (公共成員函式) |
擦除元素 (公共成員函式) | |
交換內容 (公共成員函式) | |
(C++17) |
從容器中提取節點 (公共成員函式) |
(C++17) |
從另一個容器拼接節點 (公共成員函式) |
查詢 | |
訪問指定的元素,帶邊界檢查 (公共成員函式) | |
訪問或插入指定元素 (公共成員函式) | |
返回匹配特定鍵的元素數量 (公共成員函式) | |
查詢具有特定鍵的元素 (公共成員函式) | |
(C++20) |
檢查容器是否包含具有特定鍵的元素 (公共成員函式) |
返回與特定鍵匹配的元素範圍 (公共成員函式) | |
桶介面 | |
返回指向指定桶開頭的迭代器 (公共成員函式) | |
返回指向指定桶末尾的迭代器 (公共成員函式) | |
返回桶的數量 (公共成員函式) | |
返回最大桶數量 (公共成員函式) | |
返回特定桶中的元素數量 (公共成員函式) | |
返回特定鍵的桶 (公共成員函式) | |
雜湊策略 | |
返回每個桶的平均元素數量 (公共成員函式) | |
管理每個桶的最大平均元素數量 (公共成員函式) | |
至少保留指定數量的桶並重新生成雜湊表 (公共成員函式) | |
為至少指定數量的元素保留空間並重新生成雜湊表 (公共成員函式) | |
觀察器 | |
返回用於雜湊鍵的函式 (公共成員函式) | |
返回用於比較鍵是否相等的函式 (公共成員函式) |
[編輯] 非成員函式
(C++11起)(C++11起)(C++20中移除) |
比較 unordered_map 中的值 (函式模板) |
特化 std::swap 演算法 (函式模板) | |
(C++20) |
擦除所有滿足特定標準的元素 (函式模板) |
推導指引 |
(C++17 起) |
[編輯] 注
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的範圍構造和插入 |
__cpp_lib_constexpr_containers |
202502L |
(C++26) | constexpr std::unordered_map |
[編輯] 示例
#include <iostream> #include <string> #include <unordered_map> int main() { // Create an unordered_map of three strings (that map to strings) std::unordered_map<std::string, std::string> u = { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} }; // Helper lambda function to print key-value pairs auto print_key_value = [](const auto& key, const auto& value) { std::cout << "Key:[" << key << "] Value:[" << value << "]\n"; }; std::cout << "Iterate and print key-value pairs of unordered_map, being\n" "explicit with their types:\n"; for (const std::pair<const std::string, std::string>& n : u) print_key_value(n.first, n.second); std::cout << "\nIterate and print key-value pairs using C++17 structured binding:\n"; for (const auto& [key, value] : u) print_key_value(key, value); // Add two new entries to the unordered_map u["BLACK"] = "#000000"; u["WHITE"] = "#FFFFFF"; std::cout << "\nOutput values by key:\n" "The HEX of color RED is:[" << u["RED"] << "]\n" "The HEX of color BLACK is:[" << u["BLACK"] << "]\n\n"; std::cout << "Use operator[] with non-existent key to insert a new key-value pair:\n"; print_key_value("new_key", u["new_key"]); std::cout << "\nIterate and print key-value pairs, using `auto`;\n" "new_key is now one of the keys in the map:\n"; for (const auto& n : u) print_key_value(n.first, n.second); }
可能的輸出
Iterate and print key-value pairs of unordered_map, being explicit with their types: Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000] Iterate and print key-value pairs using C++17 structured binding: Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000] Output values by key: The HEX of color RED is:[#FF0000] The HEX of color BLACK is:[#000000] Use operator[] with non-existent key to insert a new key-value pair: Key:[new_key] Value:[] Iterate and print key-value pairs, using `auto`; new_key is now one of the keys in the map: Key:[new_key] Value:[] Key:[WHITE] Value:[#FFFFFF] Key:[BLACK] Value:[#000000] Key:[BLUE] Value:[#0000FF] Key:[GREEN] Value:[#00FF00] Key:[RED] Value:[#FF0000]
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2050 | C++11 | reference 、const_reference 、pointer 和 const_pointer 的定義基於 allocator_type |
基於 value_type 和std::allocator_traits |
[編輯] 參閱
(C++11) |
鍵值對集合,按鍵雜湊 (類模板) |
鍵值對集合,按鍵排序,鍵唯一 (類模板) | |
(C++23) |
適配兩個容器以提供鍵值對集合,按唯一鍵排序 (類模板) |