跳到主要内容

std::unordered_map try_emplace() 方法

// (1) Non const version only
template< class... Args >
pair<iterator, bool> try_emplace( const Key& k, Args&&... args );

// (2) Non const version only
template< class... Args >
pair<iterator, bool> try_emplace( Key&& k, Args&&... args );

// (3) Non const version only
template< class... Args >
iterator try_emplace( const_iterator hint, const Key& k, Args&&... args );

// (4) Non const version only
template< class... Args >
iterator try_emplace( const_iterator hint, Key&& k, Args&&... args );

如果容器中不存在键为 k 的元素,则使用 args 构建元素并将其插入容器。

  • (1) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(k),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (2) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(std::move(k)),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (3) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace_hint(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(k),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (4) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace_hint(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(std::move(k)),
    std::forward_as_tuple(std::forward<Args>(args)...))
danger

如果由于插入而发生重新哈希,则所有迭代器都将失效。

否则,迭代器不受影响。引用不会失效。

仅当新元素数量大于 max_load_factor() * bucket_count() 时,才会发生重新哈希。

参数

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

返回值

复杂度

异常

如果任何操作抛出异常,此函数不产生任何影响(强异常保证)。

备注

insert()emplace() 不同,如果插入未发生,这些函数不会从右值参数移动,这使得操作值为移动独占类型的映射(例如 std::unordered_map<std::string, std::unique_ptr<foo>>)变得容易。

此外,与 emplace 不同,emplace 将键和 mapped_type 的参数分开处理,而 emplace 要求参数来构造 value_type(即 std::pair)。

功能测试宏:__cpp_lib_unordered_map_try_emplace

示例

Main.cpp
#include <iostream>
#include <utility>
#include <string>

#include <unordered_map>

auto print_node = [](const auto &node) {
std::cout << "[" << node.first << "] = " << node.second << '\n';
};

auto 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
本文来源于 CppReference 页面。可能出于改进或编辑偏好进行了修改。点击“编辑此页面”查看对本文档所做的所有更改。
悬停查看原始许可证。

std::unordered_map try_emplace() 方法

// (1) Non const version only
template< class... Args >
pair<iterator, bool> try_emplace( const Key& k, Args&&... args );

// (2) Non const version only
template< class... Args >
pair<iterator, bool> try_emplace( Key&& k, Args&&... args );

// (3) Non const version only
template< class... Args >
iterator try_emplace( const_iterator hint, const Key& k, Args&&... args );

// (4) Non const version only
template< class... Args >
iterator try_emplace( const_iterator hint, Key&& k, Args&&... args );

如果容器中不存在键为 k 的元素,则使用 args 构建元素并将其插入容器。

  • (1) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(k),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (2) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(std::move(k)),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (3) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace_hint(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(k),
    std::forward_as_tuple(std::forward<Args>(args)...))
  • (4) 如果容器中已存在与 k 等效的键,则不执行任何操作。否则,其行为类似于 emplace_hint(),但元素将使用

    value_type(std::piecewise_construct,
    std::forward_as_tuple(std::move(k)),
    std::forward_as_tuple(std::forward<Args>(args)...))
danger

如果由于插入而发生重新哈希,则所有迭代器都将失效。

否则,迭代器不受影响。引用不会失效。

仅当新元素数量大于 max_load_factor() * bucket_count() 时,才会发生重新哈希。

参数

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

返回值

复杂度

异常

如果任何操作抛出异常,此函数不产生任何影响(强异常保证)。

备注

insert()emplace() 不同,如果插入未发生,这些函数不会从右值参数移动,这使得操作值为移动独占类型的映射(例如 std::unordered_map<std::string, std::unique_ptr<foo>>)变得容易。

此外,与 emplace 不同,emplace 将键和 mapped_type 的参数分开处理,而 emplace 要求参数来构造 value_type(即 std::pair)。

功能测试宏:__cpp_lib_unordered_map_try_emplace

示例

Main.cpp
#include <iostream>
#include <utility>
#include <string>

#include <unordered_map>

auto print_node = [](const auto &node) {
std::cout << "[" << node.first << "] = " << node.second << '\n';
};

auto 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
本文来源于 CppReference 页面。可能出于改进或编辑偏好进行了修改。点击“编辑此页面”查看对本文档所做的所有更改。
悬停查看原始许可证。