std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[]
來自 cppreference.com
T& operator[]( const Key& key ); |
(1) | (C++11 起) |
T& operator[]( Key&& key ); |
(2) | (C++11 起) |
template< class K > T& operator[]( K&& x ); |
(3) | (C++26 起) |
返回一個對與 `key` 或 `x` 等價的鍵所對映的值的引用,如果該鍵不存在,則執行插入操作。
1) 如果鍵不存在,則原地構造一個
value_type
物件,使用 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 構造。 等價於 return this->try_emplace(key).first->second;。(C++17 起)當使用預設分配器時,這將導致鍵從 `key` 複製構造,並且對映的值被 值初始化。
-value_type 必須可從 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 就位構造(EmplaceConstructible)。當使用預設分配器時,這意味著 key_type 必須是 可複製構造(CopyConstructible) 的,並且 mapped_type 必須是 可預設構造(DefaultConstructible) 的。 |
2) 如果鍵不存在,則原地構造一個
value_type
物件,使用 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 構造。 等價於 return this->try_emplace(std::move(key)).first->second;。(C++17 起)
當使用預設分配器時,這將導致鍵從 `key` 移動構造,並且對映的值被 值初始化。
當使用預設分配器時,這將導致鍵從 `key` 移動構造,並且對映的值被 值初始化。
-value_type 必須可從 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 就位構造(EmplaceConstructible)。當使用預設分配器時,這意味著 key_type 必須是 可移動構造(MoveConstructible) 的,並且 mapped_type 必須是 可預設構造(DefaultConstructible) 的。 |
等價於 return this->try_emplace(std::forward<K>(x)).first->second;。此過載僅在 Hash::is_transparent 和 KeyEqual::is_transparent 有效且各自表示一個型別時才參與過載決議。這假定此類
Hash
可以使用 K
和 Key
型別呼叫,並且 KeyEqual
是透明的,這共同允許在不構造 Key
例項的情況下呼叫此函式。如果操作後元素的新數量大於舊的 max_load_factor()
*
bucket_count()
,則會發生重新雜湊。
如果發生重新雜湊(由於插入),所有迭代器都將失效。否則(沒有重新雜湊),迭代器不會失效。
目錄 |
[編輯] 引數
key | - | 要查詢的元素的鍵 |
x | - | 可與鍵透明比較的任何型別的值 |
[編輯] 返回值
1,2) 如果不存在鍵為 key 的元素,則返回新元素對映值的引用。否則,返回鍵等價於 key 的現有元素的對映值的引用。
3) 如果不存在與值 x 比較等價的鍵的元素,則返回新元素對映值的引用。否則,返回鍵與 x 比較等價的現有元素的對映值的引用。
[編輯] 異常
如果任何操作丟擲異常,則插入無效。
[編輯] 複雜度
平均情況:常數時間,最壞情況:與大小呈線性關係。
[編輯] 注意
在已釋出的 C++11 和 C++14 標準中,此函式要求 `mapped_type` 必須是 可預設插入(DefaultInsertable) 的,並且 key_type
必須是 可複製插入(CopyInsertable) 或 可移動插入(MoveInsertable) 到 *this 中的。此規範存在缺陷,已透過 LWG issue 2469 修復,上述描述已包含該問題的解決方案。
然而,已知一個實現(libc++)透過兩次獨立的分配器 `construct()` 呼叫來構造 key_type
和 `mapped_type` 物件,這可以說符合已釋出的標準要求,而不是就地構造 value_type
物件。
operator[] 是非 const 的,因為它會在鍵不存在時插入鍵。如果不需要此行為,或者容器是 const 的,則可以使用 at
。
|
(C++17 起) |
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_associative_heterogeneous_insertion |
202311L |
(C++26) | 有序和 無序關聯 容器中剩餘成員函式的異構過載。(3) |
[編輯] 示例
執行此程式碼
#include <iostream> #include <string> #include <unordered_map> void println(auto const comment, auto const& map) { std::cout << comment << '{'; for (const auto& pair : map) std::cout << '{' << pair.first << ": " << pair.second << '}'; std::cout << "}\n"; } int main() { std::unordered_map<char, int> letter_counts{{'a', 27}, {'b', 3}, {'c', 1}}; println("letter_counts initially contains: ", letter_counts); letter_counts['b'] = 42; // updates an existing value letter_counts['x'] = 9; // inserts a new value println("after modifications it contains: ", letter_counts); // count the number of occurrences of each word // (the first call to operator[] initialized the counter with zero) std::unordered_map<std::string, int> word_map; for (const auto& w : {"this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax"}) ++word_map[w]; word_map["that"]; // just inserts the pair {"that", 0} for (const auto& [word, count] : word_map) std::cout << count << " occurrence(s) of word '" << word << "'\n"; }
可能的輸出
letter_counts initially contains: {{a: 27}{b: 3}{c: 1}} after modifications it contains: {{a: 27}{b: 42}{c: 1}{x: 9}} 2 occurrence(s) of word 'a' 1 occurrence(s) of word 'hoax' 2 occurrence(s) of word 'is' 1 occurrence(s) of word 'not' 3 occurrence(s) of word 'sentence' 0 occurrence(s) of word 'that' 2 occurrence(s) of word 'this'
[編輯] 另請參閱
訪問指定的元素,帶邊界檢查 (public member function) | |
(C++17) |
插入元素或如果鍵已存在則賦值給當前元素 (public member function) |
(C++17) |
如果鍵不存在則原地插入,如果鍵存在則不執行任何操作 (public member function) |