std::flat_map::insert(3)
π std::flat_map::insert β C++ Standard Library π
πΈ NAME πΈ
std::flat_map::insert β okay bestie, this is literally the insert member function for std::flat_map, and we're about to make it make sense. β¨
β¨ Synopsis β¨
All ten overloads are since C++23, and here's the lineup exactly as it appears:
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 > (5)
(since C++23)
std::pair<iterator, bool> insert( P&& x );
template< class P > (6)
(since C++23)
iterator insert( const_iterator pos, P&& x );
template< class InputIt > (7)
(since C++23)
void insert( InputIt first, InputIt last );
template< class InputIt > (8)
(since C++23)
void insert( std::sorted_unique_t, InputIt first, InputIt last );
void insert( std::initializer_list<key_type> ilist ); (9)
(since C++23)
void insert( std::sorted_unique_t s,
(10) (since C++23)
std::initializer_list<key_type> ilist );
π¦ DESCRIPTION π¦
Core vibe: insert element(s) into the container, if the container does not already contain an element with an equivalent key. No duplicate-key chaos allowed, periodt. π
1) Inserts value. Equivalent to return emplace(value);.
2) Inserts value. Equivalent to return emplace(std::move(value));.
3) Inserts value in the position as close as possible to the position just prior to pos. Equivalent to return emplace_hint(pos, value);.
4) Inserts value in the position as close as possible to the position just prior to pos. Equivalent to return emplace_hint(pos, std::move(value));.
5) If *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.
6) If *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.
7) Inserts elements from range [first, last) as if performing the following operations sequentially:
- Step 1: Adds elements to
cas if by:
for (; 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));
}
- Step 2: Sorts the range of newly inserted elements with respect to
value_comp. - Step 3: Merges the resulting sorted range and the sorted range of pre-existing elements into a single sorted range.
- Step 4: 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());
May allocate memory during the in-place merge operation. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844). π
8) Inserts elements from range [first, last) as if performing the following operations sequentially:
- Step 1: Adds elements to
cas if by:
for (; 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));
}
- Step 2: Merges the sorted range of newly added elements and the sorted range of pre-existing elements into a single sorted range.
- Step 3: 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());
May allocate memory during the in-place merge operation. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844). π«Ά
9) Inserts elements from initializer list ilist. Equivalent to insert(ilist.begin(), ilist.end());. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844).
10) Inserts elements from initializer list ilist. Equivalent to insert(s, ilist.begin(), ilist.end());. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending LWG2844).
π Information on iterator invalidation is copied from here.
π 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 tovalue_comp()) and contains only unique elements.
π Type requirements π
InputItmust meet the requirements ofLegacyInputIterator.
π Return value π
- 1, 2) A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a
boolvalue set totrueif and only if the insertion took place. - 3, 4) An iterator to the inserted element, or to the element that prevented the insertion.
- 5) A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a
boolvalue set totrueif and only if the insertion took place. - 6) An iterator to the inserted element, or to the element that prevented the insertion.
- 7-10) (none)
π Exceptions π
- 1-6) If an exception is thrown by any operation, the insertion has no effect.
This section is incomplete. Reason: cases 7-10.
π₯ Complexity π₯
- 1-6) Linear in
size(). - 7)
N + MΒ·log(M), where N is thesize()before the operation and M isstd::distance(first, last). - 8) Linear in
size(). - 9)
N + MΒ·log(M), where N is thesize()before the operation and M isilist.size(). - 10) Linear in N, where N is
size()after the operation.
π§ 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. iykyk π
π Example π
This section is incomplete. Reason: no example.
π See also π
emplaceβ constructs element in-place (public member function).emplace_hintβ constructs elements in-place using a hint (public member function).insert_or_assignβ inserts an element or assigns to the current element if the key already exists (public member function).inserterβ creates astd::insert_iteratorof type inferred from the argument (function template).
π·οΈ Categories π·οΈ
- Todo with reason
- Todo no example
Source/version/date sign-off: this page is from http://cppreference.com, version/date 2024.06.10, and it's the std::flat_map::insert(3) page β mwah, bestie ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms