命名空间
变体
操作

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

来自 cppreference.cn
< 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 起)
(公共成员函数) [编辑]