std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::insert_or_assign

1,2) If a key equivalent to k already exists in the container, assigns std::forward(obj) to the mapped_type 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(obj)),(4,5) try_emplace(hint, std::forward<decltype(k)>(k), std::forward(obj)).

# Declarations

template< class M >
std::pair<iterator, bool> insert_or_assign( const key_type& k, M&& obj );

(since C++23)

template< class M >
std::pair<iterator, bool> insert_or_assign( key_type&& k, M&& obj );

(since C++23)

template< class K, class M >
std::pair<iterator, bool> insert_or_assign( K&& k, M&& obj );

(since C++23)

template< class M >
iterator insert_or_assign( const_iterator hint, const key_type& k, M&& obj );

(since C++23)

template< class M >
iterator insert_or_assign( const_iterator hint, key_type&& k, M&& obj );

(since C++23)

template< class K, class M >
iterator insert_or_assign( const_iterator hint, K&& k, M&& obj );

(since C++23)

# Parameters

# Notes

insert_or_assign returns more information than operator[] and does not require default-constructibility of the mapped type.

# Example

#include <flat_map>
#include <iostream>
#include <string>
 
void print_node(const auto& node)
{
    std::cout << '[' << node.first << "] = " << node.second << '\n';
}
 
void print_result(auto const& 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 (const auto& node : map)
        print_node(node);
}

# See also