Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
object::insert_or_assign

Insert an element or assign to the current element if the key already exists.

Synopsis
template<
    class M>
std::pair< iterator, bool >
insert_or_assign(
    string_view key,
    M&& m);
Description

If the key equivalent to key already exists in the container. assigns std::forward<M>(obj) to the mapped type corresponding to the key. Otherwise, inserts the new value at the end as if by insert, constructing it from value_type(key, std::forward<M>(obj)). If the insertion occurs and results in a rehashing of the container, all iterators are invalidated. Otherwise, iterators are not affected. References are not invalidated. Rehashing occurs only if the new number of elements is greater than capacity().

Complexity

Amortized constant on average, worst case linear in size().

Exception Safety

Strong guarantee. Calls to memory_resource::allocate may throw.

Return Value

A std::pair where first is an iterator to the existing or inserted element, and second is true if the insertion took place or false if the assignment took place.

Parameters

Name

Description

key

The key used for lookup and insertion

m

The value to insert or assign

Exceptions

Type

Thrown On

std::length_error

if key is too long


PrevUpHomeNext