From cppreference.com
template<classM>std::pair<iterator,bool>insert_or_assign(constkey_type&k,M&&obj); (1) (since C++23)template<classM>std::pair<iterator,bool>insert_or_assign(key_type&&k,M&&obj); (2) (since C++23)template<classK,classM>std::pair<iterator,bool>insert_or_assign(K&&k,M&&obj); (3)(since C++23)template<classM>iteratorinsert_or_assign(const_iteratorhint,constkey_type&k,M&&obj); (4) (since C++23)template<classM>iteratorinsert_or_assign(const_iteratorhint,key_type&&k,M&&obj); (5) (since C++23)template<classK,classM>iteratorinsert_or_assign(const_iteratorhint,K&&k,M&&obj); (6)(since C++23)1,2) If a key equivalent to k already exists in the container, assigns std::forward<M>(obj) to the
corresponding to the key k. If the key does not exist, inserts the new value as if by
(1,2)try_emplace(std::forward<decltype(k)>(k),std::forward<M>(obj)),
(4,5)try_emplace(hint,std::forward<decltype(k)>(k),std::forward<M>(obj)).
The program is ill-formed if either std::is_assignable_v<mapped_type&,M> or std::is_constructible_v<mapped_type,M> is false.
3,6) If a key equivalent to k already exists in the container, assigns std::forward<M>(obj) to the
corresponding to the key k. Otherwise, equivalent to
(3)try_emplace(std::forward<K>(k),std::forward<M>(obj)),
(6)try_emplace(hint,std::forward<K>(k),std::forward<M>(obj)).
The conversion from k into key_type must construct an object u, for which find(k)==find(u) is true. Otherwise, the behavior is undefined.
These overloads participate in overload resolution only if :
The qualified-id Compare::is_transparent is valid and denotes a type.
std::is_constructible_v<key_type,K> is true.
std::is_assignable_v<mapped_type&,M> is true.
std::is_constructible_v<mapped_type,M> is true.
Parameters
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 obj - the value to insert or assign Return value
1-3) The bool component is true if the insertion took place and false if the assignment took place. The iterator component is pointing at the element that was inserted or updated.
4-6) Iterator pointing at the element that was inserted or updated.
Complexity
Notes
insert_or_assign returns more information than
[] and does not require default-constructibility of the mapped type.
Example
Run this code
#include<flat_map>#include<iostream>#include<string>voidprint_node(constauto&node){std::cout<<'['<<node.first<<"] = "<<node.second<<'\n';}voidprint_result(autoconst&pair){std::cout<<(pair.second?"inserted: ":"assigned: ");print_node(*pair.first);}intmain(){std::flat_map<std::string,std::string>map;print_result(map.insert_or_assign("a","apple"));print_result(map.insert_or_assign("b","banana"));print_result(map.insert_or_assign("c","cherry"));print_result(map.insert_or_assign("c","clementine"));for(constauto&node:map)print_node(node);}Output:
inserted: [a] = apple inserted: [b] = banana inserted: [c] = cherry assigned: [c] = clementine [a] = apple [b] = banana [c] = clementine See also
access or insert specified element
(public member function)
access specified element with bounds checking
(public member function)
inserts elements
(public member function)
constructs element in-place
(public member function)
inserts in-place if the key does not exist, does nothing if the key exists
(public member function)