在线文档教程
C++
容器 | Containers

std::map::insert_or_assign

STD::map::插入[医]或[医]指派

template pair insert_or_assign(const key_type& k, M&& obj(1)(since C++17)
template <class M> pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj(2)(since C++17)
template <class M> iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj(3)(since C++17)
template <class M> iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj(4)(since C++17)

1,3%29,如果密钥相当于k已存在于容器中,则分配std::forward<M>(obj)到mapped_type对应键k如果键不存在,则插入新值,就像插入,从value_type(k,std::forward<M>(obj))

2,4%29与%281,3%29相同,但映射值是从value_type(std::move(k),std::forward<M>(obj))

没有迭代器或引用无效。如果插入成功,则在节点句柄中保存时获得的元素的指针和引用无效,并且在提取该元素之前获得的指针和引用变得有效。%28自C++17%29。

参数

k-the key used both to look up and to insert if not found
hint-iterator to the position before which the new element will be inserted
args-arguments to forward to the constructor of the element

返回值

1,2%29 bool组分是true如果插入发生了false如果任务发生了。迭代器组件指向插入或更新的元素。

3,4%29指向插入或更新的元素的Iterator

复杂性

1,2%29与座落

3.4%29与座落[医]暗示

注记

insert_or_assign返回比operator[]并且不要求映射类型的默认可构造性。

另见

operator[]access specified element (public member function)
at (C++11)access specified element with bounds checking (public member function)
insertinserts elements or nodes (since C++17) (public member function)
emplace (C++11)constructs element in-place (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/容器/map/INSERT[医]或[医]指派