std::flat_set::insert(3)
std::flat_set::insert(3)
okay bestie, welcome to the C++ Standard Libary corner of the manual β yes, thatβs βLibaryβ with the typo, Iβm just reporting the header, not fixing it π. This page is all about std::flat_set::insert, and the whole vibe is: put element(s) into the container if there isnβt already an equivalent key chilling in there. Every single overload on this page is since C++23, so sheβs new, sheβs fresh, sheβs giving modern-standard energy π¦.
β¨ NAME β¨
std::flat_set::insert β std::flat_set::insert (yes, the name section is literally the same thing twice, and honestly? iconic π
)
π Synopsis π
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 K > (5)
(since C++23)
iterator insert( const_iterator pos, K&& x );
template< class InputIt > (6)
(since C++23)
void insert( InputIt first, InputIt last );
template< class K > (7)
(since C++23)
std::pair<iterator, bool> insert( K&& x );
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 πΈ
Inserts element(s) into the container, if the container does not already contain an element with an equivalent key. Thatβs the whole mood, bestie β no duplicate equivalent keys allowed, periodt.
- 1) Inserts
value. Equivalent toreturn emplace(value);. - 2) Inserts
value. Equivalent toreturn emplace(std::move(value));. - 3) Inserts
valuein the position as close as possible to the position just prior topos. Equivalent toreturn emplace_hint(pos, value);. - 4) Inserts
valuein the position as close as possible to the position just prior topos. Equivalent toreturn emplace_hint(pos, std::move(value));. - 5, 7) If
*thisalready contains an element which transparently compares equivalent tox, does nothing. Otherwise, inserts a new element as if by:- (5)
emplace(pos, std::forward<K>(x))(in the position as close as possible to the position just prior topos); - (7)
emplace(std::forward<K>(x)).
- (5)
The conversion from x into key_type must construct an object u, for which find(k) == find(u) is true. Otherwise, the behavior is undefined. Like, genuinely undefined β do not play with that.
These overloads participate in overload resolution only if
- The qualified-id
Compare::is_transparentis valid and denotes a type, and std::is_constructible_v<value_type, K>is true,
which together allows calling this function without constructing an instance of Key.
- 6) Equivalent to the sequence of operations:
- Inserts elements from range
[first, last)as if byc.insert(c.end(), first, last);. - Sorts the range of newly inserted elements with respect to
compare. - Merges the resulting sorted range and the sorted range of pre-existing elements into a single sorted range. (Note: the merge operation may allocate memory).
- Erases all but the first element from each group of consecutive equivalent elements.
- Inserts elements from range
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). Equivalent toinsert(first, last);. 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 toinsert(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 toinsert(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β iterator to the position before which the new element will be insertedvalueβ element value to insertfirst,lastβ range of elements to insertilistβ initializer list to insert the values fromxβ a value of any type that can be transparently compared with a keysβ a disambiguation tag indicating that the input sequence is sorted (with respect tocompare) 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β5) An iterator to the inserted element, or to the element that prevented the insertion.
- 6) (none)
- 7) 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. - 8β10) (none)
π Exceptions π
- 1β5, 7) If an exception is thrown by any operation, the insertion has no effect.
This section is incomplete Reason: cases 6,8-10
π₯ Complexity π₯
- 1β5) Linear in
size(). - 6)
N + MΒ·log(M), whereNis thesize()before the operation andMisstd::distance(first, last). - 7) Linear in
size(). - 8) Linear in
N, whereNissize()after the operation. - 9)
N + MΒ·log(M), whereNis thesize()before the operation andMisilist.size(). - 10) Linear in
N, whereNissize()after the operation.
This section is incomplete Reason: recheck the complexity: 1-5, 7, 9, 10. The lookup is log(N), but insertion in underlying container is N/2 on average => Linear.
β Notes β
The hinted insert (3-5) 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. No cap, that little size() check is the move if youβre nosy about whether the hint actually inserted.
π Example π
// Run this code
#include <cassert>
#include <flat_set>
#include <iostream>
int main()
{
std::flat_set<int> set;
auto result_1 = set.insert(3);
assert(result_1.first != set.end()); // it is a valid iterator
assert(*result_1.first == 3);
if (result_1.second)
std::cout << "insert done\n";
auto result_2 = set.insert(3);
assert(result_2.first == result_1.first); // the same iterator
assert(*result_2.first == 3);
if (!result_2.second)
std::cout << "no insertion\n";
}
Output:
insert done
no insertion
π See also π
emplaceβ constructs element in-place (public member function)emplace_hintβ constructs elements in-place using a hint (public member function)inserterβ creates astd::insert_iteratorof type inferred from the argument (function template)
π·οΈ Category π·οΈ
- * Todo with reason
xoxo, your C++ bestie π β source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::flat_set::insert(3)
π¦ 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