std::map insert_or_assign() method
- od C++17
// (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) If a key equivalent to k already exists in the container,
assigns
std::forward<M>(obj)to themapped_typecorresponding to the keyk. If the key does not exist, inserts the new value as if byinsert(), constructing it fromvalue_type(k, std::forward<M>(obj)). - (2-4) Same as (1-3), except the mapped value is constructed from
value_type(std::move(k), std::forward<M>(obj)).
- od C++20
- do C++20
The program is ill-formed if
std::is_assignable_v<mapped_type&, M&&> is false The behaviour is undefined if
std::is_assignable_v<mapped_type&, M&&> is false No iterators or references are invalidated.
Parameters
key- the key used both to look up and to insert if not foundhint- iterator to the position before which the new element will be insertedobj- the value to insert or assign