命名空间
变体
操作

std::map<Key,T,Compare,Allocator>::try_emplace

来自 cppreference.cn
< cpp‎ | 容器‎ | map
 
 
 
 
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 无法从相应的表达式中 EmplaceConstructiblemap 中,则行为未定义。
3) 此重载仅在满足以下所有条件时才参与重载决议
如果 equal_range(u.first) == equal_range(k)false,则行为未定义,其中 u 是要插入的新元素。
6) 此重载仅在限定ID Compare::is_transparent 有效且表示一个类型时才参与重载决议。
如果 equal_range(u.first) == equal_range(k)false,则行为未定义,其中 u 是要插入的新元素。

迭代器或引用均未失效。

目录

[edit] 参数

k - 用于查找和插入(如果未找到)的键
hint - 指向新元素将插入位置之前的迭代器
args - 转发给元素构造函数的参数

[edit] 返回值

1-3)emplace 相同
一个 pair,由指向插入元素的迭代器(或阻止插入的元素)和一个 bool 值组成,该值仅在插入发生时设置为 true
4-6)emplace_hint 相同
一个迭代器,指向插入的元素或阻止插入的元素。

[edit] 复杂度

1-3)emplace 相同
容器大小的对数级别。
4-6)emplace_hint 相同
通常为容器大小的对数级,但如果新元素刚好插入在 hint 之前,则为摊销常数级。

[edit] 注意

insertemplace 不同,如果未发生插入,这些函数不会从右值参数移动,这使得操作值是仅可移动类型的 map 变得容易,例如 std::map<std::string, std::unique_ptr<foo>>。此外,try_emplace 独立处理键和 mapped_type 的参数,这与 emplace 不同,后者要求参数构造一个 value_type(即 std::pair)。

重载 (3,6) 可以在不构造 Key 类型对象的情况下调用。

特性测试 标准 特性
__cpp_lib_map_try_emplace 201411L (C++17) std::map::try_emplace, std::map::insert_or_assign
__cpp_lib_associative_heterogeneous_insertion 202311L (C++26) 有序无序关联容器中剩余成员函数的异构重载。重载 (3)(6)

[edit] 示例

#include <iostream>
#include <string>
#include <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::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

[edit] 参阅

(C++11)
就地构造元素
(公共成员函数) [编辑]
使用提示就地构造元素
(公共成员函数) [编辑]
插入元素 或节点(C++17 起)
(公共成员函数) [编辑]