(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
template<class M> std::pair<iterator,bool> insert_or_assign(const key_type& k, M&& obj); | (1) | (since C++23) |
template<class M> std::pair<iterator,bool> insert_or_assign( key_type&& k, M&& obj); | (2) | (since C++23) |
template<class K,class M> std::pair<iterator,bool> insert_or_assign( K&& k, M&& obj); | (3) | (since C++23) |
template<class M> iterator insert_or_assign( const_iterator hint,const key_type& k, M&& obj); | (4) | (since C++23) |
template<class M> iterator insert_or_assign( const_iterator hint, key_type&& k, M&& obj); | (5) | (since C++23) |
template<class K,class M> iterator insert_or_assign( const_iterator hint, K&& k, M&& obj); | (6) | (since C++23) |
mapped_type
corresponding to the keyk. If the key does not exist, inserts the new value as if bymapped_type
corresponding to the keyk. Otherwise, equivalent tokey_type
must construct an objectu, for whichfind(k)== find(u) istrue. Otherwise, the behavior is undefined.Compare::is_transparent
is valid and denotes a type.Information on iterator invalidation is copied fromhere |
Contents |
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 |
emplace
.emplace_hint
.insert_or_assign
returns more information thanoperator
[] and does not require default-constructibility of the mapped type.
#include <flat_map>#include <iostream>#include <string> void print_node(constauto& node){std::cout<<'['<< node.first<<"] = "<< node.second<<'\n';} void print_result(autoconst& pair){std::cout<<(pair.second?"inserted: ":"assigned: "); print_node(*pair.first);} int main(){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] = appleinserted: [b] = bananainserted: [c] = cherryassigned: [c] = clementine[a] = apple[b] = banana[c] = clementine
access or insert specified element (public member function)[edit] | |
access specified element with bounds checking (public member function)[edit] | |
inserts elements (public member function)[edit] | |
constructs element in-place (public member function)[edit] | |
inserts in-place if the key does not exist, does nothing if the key exists (public member function)[edit] |