名稱空間
變體
操作

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert_or_assign

來自 cppreference.com
< cpp‎ | 容器‎ | 無序對映
 
 
 
 
template< class M >
std::pair<iterator, bool> insert_or_assign( const Key& k, M&& obj );
(1) (C++17 起)
template< class M >
std::pair<iterator, bool> insert_or_assign( Key&& k, M&& obj );
(2) (C++17 起)
template< class K, class M >
std::pair<iterator, bool> insert_or_assign( K&& k, M&& obj );
(3) (C++26 起)
template< class M >
iterator insert_or_assign( const_iterator hint, const Key& k, M&& obj );
(4) (C++17 起)
template< class M >
iterator insert_or_assign( const_iterator hint, Key&& k, M&& obj );
(5) (C++17 起)
template< class K, class M >
iterator insert_or_assign( const_iterator hint, K&& k, M&& obj );
(6) (C++26 起)
1,4) 如果容器中已經存在與 k 等效的鍵,則將 std::forward<M>(obj) 賦值給與鍵 k 對應的 mapped_type。如果鍵不存在,則插入新值,如同透過 insert,從 value_type(k, std::forward<M>(obj)) 構造它。
2,5)(1,4) 相同,除了對映值是從 value_type(std::move(k), std::forward<M>(obj)) 構造的。
3,6) 如果容器中已經存在與 k 等效的鍵,則將 std::forward<M>(obj) 賦值給與鍵 k 對應的 mapped_type。如果鍵不存在,則構造一個 value_type 型別的物件 u,其值為 std::forward<K>(k), std::forward<M>(obj)),然後將 u 插入到 *this 中。如果 hash_function()(u.first) != hash_function()(k) || contains(u.first)true,則行為未定義。value_type 必須是 可就地構造unordered_map 中,從 std::forward<K>(k), std::forward<M>(obj)。此過載僅當 Hash::is_transparentKeyEqual::is_transparent 有效且各自表示一個型別時才參與過載決議。這假定此類 Hash 可以同時用 KKey 型別呼叫,並且 KeyEqual 是透明的,這共同允許在不構造 Key 例項的情況下呼叫此函式。

行為未定義(直到 C++20)程式格式錯誤(自 C++20 起),如果 std::is_assignable_v<mapped_type&, M&&>false

如果操作後元素的新數量大於舊的 max_load_factor() * bucket_count(),則會進行重新雜湊。
如果發生重新雜湊(由於插入),所有迭代器都將失效。否則(沒有重新雜湊),迭代器不會失效。

目錄

[編輯] 引數

k - 用於查詢和插入(如果未找到)的鍵
hint - 指向新元素將插入位置之前的迭代器
obj - 要插入或賦值的值

[編輯] 返回值

1-3) bool 元件為 true 表示進行了插入,為 false 表示進行了賦值。迭代器元件指向插入或更新的元素。
4-6) 指向插入或更新元素的迭代器。

[編輯] 複雜度

1-3)emplace 相同。
4-6)emplace_hint 相同。

[編輯] 注意

insert_or_assign 返回的資訊比 operator[] 更多,並且不需要對映型別的預設可構造性。

特性測試 標準 特性
__cpp_lib_unordered_map_try_emplace 201411L (C++17) std::unordered_map::try_emplace,
std::unordered_map::insert_or_assign
__cpp_lib_associative_heterogeneous_insertion 202311L (C++26) 有序無序關聯容器中剩餘成員函式的多型過載。過載 (3)(6)

[編輯] 示例

#include <iostream>
#include <string>
#include <unordered_map>
 
void print_node(const auto& node)
{
    std::cout << '[' << node.first << "] = " << node.second << '\n';
}
 
void print_result(auto const& pair)
{
    std::cout << (pair.second ? "inserted: " : "assigned: ");
    print_node(*pair.first);
}
 
int main()
{
    std::unordered_map<std::string, std::string> myMap;
 
    print_result(myMap.insert_or_assign("a", "apple"));
    print_result(myMap.insert_or_assign("b", "banana"));
    print_result(myMap.insert_or_assign("c", "cherry"));
    print_result(myMap.insert_or_assign("c", "clementine"));
 
    for (const auto& node : myMap)
        print_node(node);
}

可能的輸出

inserted: [a] = apple
inserted: [b] = banana
inserted: [c] = cherry
assigned: [c] = clementine
[c] = clementine
[a] = apple
[b] = banana

[編輯] 參閱

訪問或插入指定元素
(公共成員函式) [編輯]
訪問指定的元素,帶邊界檢查
(公共成員函式) [編輯]
插入元素 或節點(C++17 起)
(公共成員函式) [編輯]
就地構造元素
(公共成員函式) [編輯]