名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 容器‎ | 無序對映
 
 
 
 
template< class... Args >
std::pair<iterator, bool> try_emplace( const Key& k, Args&&... args );
(1) (C++17 起)
template< class... Args >
std::pair<iterator, bool> try_emplace( Key&& k, Args&&... args );
(2) (C++17 起)
template< class K, class... Args >
std::pair<iterator, bool> try_emplace( K&& k, Args&&... args );
(3) (C++26 起)
template< class... Args >
iterator try_emplace( const_iterator hint, const Key& k, Args&&... args );
(4) (C++17 起)
template< class... Args >
iterator try_emplace( const_iterator hint, Key&& k, Args&&... args );
(5) (C++17 起)
template< class K, class... Args >
iterator try_emplace( const_iterator hint, K&& k, Args&&... args );
(6) (C++26 起)

如果容器中已存在與 k 等效的鍵,則不執行任何操作。否則,將一個新元素插入容器中,其鍵為 k,值使用 args 構造。在這種情況下

1) 行為類似於 emplace,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(k),

           std::forward_as_tuple(std::forward<Args>(args)...))
2) 行為類似於 emplace,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(std::move(k)),

           std::forward_as_tuple(std::forward<Args>(args)...))
3) 行為類似於 emplace,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(std::forward<K>(k)),

           std::forward_as_tuple(std::forward<Args>(args)...))
4) 行為類似於 emplace_hint,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(k),

           std::forward_as_tuple(std::forward<Args>(args)...))
5) 行為類似於 emplace_hint,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(std::move(k)),

           std::forward_as_tuple(std::forward<Args>(args)...))
6) 行為類似於 emplace_hint,但元素構造方式如下:
value_type(std::piecewise_construct,

           std::forward_as_tuple(std::forward<K>(k)),

           std::forward_as_tuple(std::forward<Args>(args)...))
1-6) 如果 value_type 不能從相應的表示式 EmplaceConstructibleunordered_map 中,則行為是未定義的。
3) 此過載僅在滿足以下所有條件時才參與過載決議
如果 hash_function()(u.first) != hash_function()(k) || contains(u.first)true,則行為是未定義的,其中 u 是要插入的新元素。
6) 此過載僅在 Hash::is_transparentKeyEqual::is_transparent 都有效且各自表示一個型別時才參與過載決議。
如果 hash_function()(u.first) != hash_function()(k) || contains(u.first)true,則行為是未定義的,其中 u 是要插入的新元素。

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

目錄

[編輯] 引數

k - 用於查詢和插入(如果未找到)的鍵
hint - 指向新元素將插入位置之前的迭代器
args - 轉發給元素建構函式的引數

[編輯] 返回值

1-3)emplace 相同
一個由指向插入元素的迭代器(或阻止插入的元素)和一個布林值組成對,當且僅當插入發生時,布林值為 true
4-6)emplace_hint 相同
一個迭代器,指向插入的元素或阻止插入的元素。

[編輯] 複雜度

1-3)emplace 相同
平均情況下攤銷常數,最壞情況下與容器大小呈線性關係。
4-6)emplace_hint 相同
平均情況下攤銷常數,最壞情況下與容器大小呈線性關係。

[編輯] 注意

insertemplace 不同,如果未發生插入,這些函式不會從右值引數進行移動,這使得操作值為僅可移動型別(例如 std::unordered_map<std::string, std::unique_ptr<foo>>)的對映變得容易。此外,try_emplace 獨立處理鍵和 mapped_type 的引數,這與 emplace 不同,後者要求引數構造一個 value_type(即 std::pair)。

過載 (3,6) 可以在不構造 Key 型別物件的情況下呼叫。

特性測試 標準 特性
__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>
#include <utility>
 
void print_node(const auto& node)
{
    std::cout << '[' << node.first << "] = " << node.second << '\n';
}
 
void print_result(auto const& pair)
{
    std::cout << (pair.second ? "inserted: " : "ignored:  ");
    print_node(*pair.first);
}
 
int main()
{
    using namespace std::literals;
    std::unordered_map<std::string, std::string> m;
 
    print_result(m.try_emplace("a", "a"s));
    print_result(m.try_emplace("b", "abcd"));
    print_result(m.try_emplace("c", 10, 'c'));
    print_result(m.try_emplace("c", "Won't be inserted"));
 
    for (const auto& p : m)
        print_node(p);
}

可能的輸出

inserted: [a] = a
inserted: [b] = abcd
inserted: [c] = cccccccccc
ignored:  [c] = cccccccccc
[a] = a
[b] = abcd
[c] = cccccccccc

[編輯] 另請參閱

就地構造元素
(公共成員函式) [編輯]
使用提示就地構造元素
(公共成員函式) [編輯]
插入元素 或節點(C++17 起)
(公共成員函式) [編輯]