跳到主要内容

std::unordered_map insert_or_assign() 方法

// (1) Non const version only
template <class M>
std::pair<iterator, bool> insert_or_assign( const Key& k, M&& obj );

// (2) Non const version only
template <class M>
std::pair<iterator, bool> insert_or_assign( Key&& k, M&& obj );

// (3) Non const version only
template <class M>
iterator insert_or_assign( const_iterator hint, const Key& k, M&& obj );

// (4) Non const version only
template <class M>
iterator insert_or_assign( const_iterator hint, Key&& k, M&& obj );
  • (1-3) 如果容器中已存在与 k 等价的键,则将 std::forward<M>(obj) 赋值给与键 k 对应的 mapped_type

    如果键不存在,则如同调用 insert() 一样插入新值,使用 value_type(k, std::forward<M>(obj)) 来构造它。

  • (2-4)(1-3) 相同,只不过映射值是用 value_type(std::move(k), std::forward<M>(obj)) 构造的。

如果 std::is_assignable_v<mapped_type&, M&&>false,则行为未定义。
失效

如果发生插入并导致容器的重新哈希,则所有迭代器都将失效。

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

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

参数

  • k - 用于查找和(如果未找到)插入的键
  • hint - 指向新元素将插入位置之前的迭代器
  • obj - 要插入或赋值的值

返回值

  • (1-2) - 如果发生了插入,则 bool 组件为 true,如果发生了赋值,则为 false。迭代器组件指向已插入或更新的元素。
  • (3-4) - 指向已插入或更新的元素的迭代器。

复杂度

异常

(无)

备注

insert_or_assign 返回的信息比 operator[] 多,并且不需要映射类型是默认可构造的。

功能测试宏:__cpp_lib_unordered_map_try_emplace

示例

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

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

auto print_result = [](auto const &pair) {
std::cout << (pair.second ? "inserted: " : "assigned: ");
print_node(*pair.first);
};

int main()
{
std::unordered_map<std::string, std::string> myMap;

print_result( myMap.insert_or_assign("a", "apple" ) );
print_result( myMap.insert_or_assign("b", "banana" ) );
print_result( myMap.insert_or_assign("c", "cherry" ) );
print_result( myMap.insert_or_assign("c", "clementine") );

for (const auto &node : myMap) { print_node(node); }
}
可能输出
inserted: [a] = apple
inserted: [b] = banana
inserted: [c] = cherry
assigned: [c] = clementine
[c] = clementine
[a] = apple
[b] = banana
本文源自 这个 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页”查看对本文档所做的所有更改。
悬停查看原始许可证。

std::unordered_map insert_or_assign() 方法

// (1) Non const version only
template <class M>
std::pair<iterator, bool> insert_or_assign( const Key& k, M&& obj );

// (2) Non const version only
template <class M>
std::pair<iterator, bool> insert_or_assign( Key&& k, M&& obj );

// (3) Non const version only
template <class M>
iterator insert_or_assign( const_iterator hint, const Key& k, M&& obj );

// (4) Non const version only
template <class M>
iterator insert_or_assign( const_iterator hint, Key&& k, M&& obj );
  • (1-3) 如果容器中已存在与 k 等价的键,则将 std::forward<M>(obj) 赋值给与键 k 对应的 mapped_type

    如果键不存在,则如同调用 insert() 一样插入新值,使用 value_type(k, std::forward<M>(obj)) 来构造它。

  • (2-4)(1-3) 相同,只不过映射值是用 value_type(std::move(k), std::forward<M>(obj)) 构造的。

如果 std::is_assignable_v<mapped_type&, M&&>false,则行为未定义。
失效

如果发生插入并导致容器的重新哈希,则所有迭代器都将失效。

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

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

参数

  • k - 用于查找和(如果未找到)插入的键
  • hint - 指向新元素将插入位置之前的迭代器
  • obj - 要插入或赋值的值

返回值

  • (1-2) - 如果发生了插入,则 bool 组件为 true,如果发生了赋值,则为 false。迭代器组件指向已插入或更新的元素。
  • (3-4) - 指向已插入或更新的元素的迭代器。

复杂度

异常

(无)

备注

insert_or_assign 返回的信息比 operator[] 多,并且不需要映射类型是默认可构造的。

功能测试宏:__cpp_lib_unordered_map_try_emplace

示例

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

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

auto print_result = [](auto const &pair) {
std::cout << (pair.second ? "inserted: " : "assigned: ");
print_node(*pair.first);
};

int main()
{
std::unordered_map<std::string, std::string> myMap;

print_result( myMap.insert_or_assign("a", "apple" ) );
print_result( myMap.insert_or_assign("b", "banana" ) );
print_result( myMap.insert_or_assign("c", "cherry" ) );
print_result( myMap.insert_or_assign("c", "clementine") );

for (const auto &node : myMap) { print_node(node); }
}
可能输出
inserted: [a] = apple
inserted: [b] = banana
inserted: [c] = cherry
assigned: [c] = clementine
[c] = clementine
[a] = apple
[b] = banana
本文源自 这个 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页”查看对本文档所做的所有更改。
悬停查看原始许可证。