std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::try_emplace
来自 cppreference.cn
                    
                                        
                    
                    
                                                            
                    
| 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) 行为类似于 
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace,但元素构造方式如下:value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
2) 行为类似于 
value_type(std::piecewise_construct,
emplace,但元素构造方式如下:value_type(std::piecewise_construct,
           std::forward_as_tuple(std::move(k)),
3) 行为类似于 
value_type(std::piecewise_construct,
emplace,但元素构造方式如下:value_type(std::piecewise_construct,
           std::forward_as_tuple(std::forward<K>(k)),
4) 行为类似于 
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace_hint,但元素构造方式如下:value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
5) 行为类似于 
value_type(std::piecewise_construct,
emplace_hint,但元素构造方式如下:value_type(std::piecewise_construct,
           std::forward_as_tuple(std::move(k)),
6) 行为类似于 
value_type(std::piecewise_construct,
emplace_hint,但元素构造方式如下:value_type(std::piecewise_construct,
           std::forward_as_tuple(std::forward<K>(k)),
3) 此重载仅在满足以下所有条件时才参与重载决议
- std::is_convertible_v<K&&, const_iterator> 和 std::is_convertible_v<K&&, iterator> 都为 false。
- Hash::is_transparent 和 KeyEqual::is_transparent 均有效且各自表示一个类型。
 如果 hash_function()(u.first) != hash_function()(k) || contains(u.first) 为 true,则行为是未定义的,其中 u 是要插入的新元素。
6) 此重载仅在 Hash::is_transparent 和 KeyEqual::is_transparent 都有效且各自表示一个类型时才参与重载决议。
 如果 hash_function()(u.first) != hash_function()(k) || contains(u.first) 为 true,则行为是未定义的,其中 u 是要插入的新元素。
如果操作后新元素数量大于旧的 max_load_factor() * bucket_count(),则会发生重新散列。
如果发生重新哈希(由于插入),所有迭代器都将失效。否则(没有重新哈希),迭代器不会失效。
| 目录 | 
[编辑] 参数
| k | - | 用于查找和插入(如果未找到)的键 | 
| hint | - | 指向新元素将插入位置之前的迭代器 | 
| args | - | 转发给元素构造函数的参数 | 
[编辑] 返回值
[编辑] 复杂度
[编辑] 注意
与 insert 或 emplace 不同,如果未发生插入,这些函数不会从右值参数进行移动,这使得操作值为仅可移动类型(例如 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 起) (公共成员函数) | 


