| std::pair<iterator, bool> insert( const value_type& value ); |
(1) | (since C++23) |
| std::pair<iterator, bool> insert( value_type&& value ); |
(2) | (since C++23) |
| iterator insert( const_iterator pos, const value_type& value ); |
(3) | (since C++23) |
| iterator insert( const_iterator pos, value_type&& value ); |
(4) | (since C++23) |
| template< class P > std::pair<iterator, bool> insert( P&& x ); |
(5) | (since C++23) |
| template< class P > iterator insert( const_iterator pos, P&& x ); |
(6) | (since C++23) |
| template< class InputIt > void insert( InputIt first, InputIt last ); |
(7) | (since C++23) |
| template< class InputIt > void insert( std::sorted_unique_t, InputIt first, InputIt last ); |
(8) | (since C++23) |
| void insert( std::initializer_list<key_type> ilist ); |
(9) | (since C++23) |
| void insert( std::sorted_unique_t s, std::initializer_list<key_type> ilist ); |
(10) | (since C++23) |
Inserts element(s) into the container, if the container does not already contain an element with an equivalent key.
value. Equivalent to return emplace(value);.value. Equivalent to return emplace(std::move(value));.value in the position as close as possible to the position just prior to pos. Equivalent to return emplace_hint(pos, value);.value in the position as close as possible to the position just prior to pos. Equivalent to return emplace_hint(pos, std::move(value));.*this already contains an element which transparently compares equivalent to x, does nothing. Otherwise, inserts x into *this as if by emplace(std::forward<P>(x));. This overload participates in overload resolution only if std::is_constructible_v<pair<key_type, mapped_type>, P> is true.*this already contains an element which transparently compares equivalent to x, does nothing. Otherwise, inserts x into *this in the position as close as possible to the position just prior to pos. Equivalent to return emplace_hint(position, std::forward<P>(x));. This overload participates in overload resolution only if std::is_constructible_v<pair<key_type, mapped_type>, P> is true.[first, last) as if performing the following operations sequentially:
- Adds elements to
cas if byfor (; first != last; ++first)
{
value_type value = *first;
c.keys.insert(c.keys.end(), std::move(value.first));
c.values.insert(c.values.end(), std::move(value.second));
} - Sorts the range of newly inserted elements with respect to
value_comp. - Merges the resulting sorted range and the sorted range of pre-existing elements into a single sorted range.
- Erases the duplicate elements as if by:
auto zv = std::views::zip(c.keys, c.values);
auto it = ranges::unique(zv, key_equiv(compare)).begin();
auto dist = std::distance(zv.begin(), it);
c.keys.erase(c.keys.begin() + dist, c.keys.end());
c.values.erase(c.values.begin() + dist, c.values.end());
[first, last) as if performing the following operations sequentially:
- Adds elements to
cas if byfor (; first != last; ++first)
{
value_type value = *first;
c.keys.insert(c.keys.end(), std::move(value.first));
c.values.insert(c.values.end(), std::move(value.second));
} - Merges the sorted range of newly added elements and the sorted range of pre-existing elements into a single sorted range.
- Erases the duplicate elements as if by:
auto zv = std::views::zip(c.keys, c.values);
auto it = ranges::unique(zv, key_equiv(compare)).begin();
auto dist = std::distance(zv.begin(), it);
c.keys.erase(c.keys.begin() + dist, c.keys.end());
c.values.erase(c.values.begin() + dist, c.values.end());
ilist. Equivalent to insert(ilist.begin(), ilist.end());.ilist. Equivalent to insert(s, ilist.begin(), ilist.end());.Parameters
| pos | - | an iterator to the position before which the new element will be inserted |
| value | - | an element value to insert |
| first, last | - | a range of elements to insert |
| ilist | - | an initializer list to insert the values from |
| x | - | a value of any type that can be transparently compared with a key |
| s | - | a disambiguation tag indicating that the input sequence is sorted (with respect to value_comp()) and contains only unique elements
|
| Type requirements | ||
InputIt must meet the requirements of LegacyInputIterator.
| ||
Return value
true if and only if the insertion took place.true if and only if the insertion took place.Exceptions
Reason: cases 7-10
Complexity
size().size().Notes
The hinted insert (3,4,6) does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such as std::vector::insert. This makes it possible to create generic inserters such as std::inserter. One way to check success of a hinted insert is to compare size() before and after.
Example
Reason: no example
See also
| constructs element in-place (public member function) | |
| constructs elements in-place using a hint (public member function) | |
| inserts an element or assigns to the current element if the key already exists (public member function) | |
| creates a std::insert_iterator of type inferred from the argument (function template) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.