跳到主要内容

std::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,则程序格式错误。

不使任何迭代器或引用失效。

参数

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

返回值

  • (1-2) 布尔组件在插入发生时为 true,在赋值发生时为 false。迭代器组件指向被插入或更新的元素。
  • (3-4) 迭代器指向被插入或更新的元素。

复杂度

异常

(无)

备注

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

特性测试宏:__cpp_lib_map_try_emplace

示例

Main.cpp
#include <iostream>
#include <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::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
[a] = apple
[b] = banana
[c] = clementine

本文档源自 此 CppReference 页面。它可能因改进或编辑偏好而有所更改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::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,则程序格式错误。

不使任何迭代器或引用失效。

参数

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

返回值

  • (1-2) 布尔组件在插入发生时为 true,在赋值发生时为 false。迭代器组件指向被插入或更新的元素。
  • (3-4) 迭代器指向被插入或更新的元素。

复杂度

异常

(无)

备注

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

特性测试宏:__cpp_lib_map_try_emplace

示例

Main.cpp
#include <iostream>
#include <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::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
[a] = apple
[b] = banana
[c] = clementine

本文档源自 此 CppReference 页面。它可能因改进或编辑偏好而有所更改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。