(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
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> iterator insert( const_iterator pos, K&& x); | (5) | (since C++23) |
template<class InputIt> void insert( InputIt first, InputIt last); | (6) | (since C++23) |
template<class K> std::pair<iterator,bool> insert( K&& x); | (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.
key_type must construct an objectu, for whichfind(k)== find(u) istrue. Otherwise, the behavior is undefined.Compare::is_transparent is valid and denotes a type, andKey.[first, last) as if byc.insert(c.end(), first, last);.compare.[first, last). Equivalent toinsert(first, last);.| Information on iterator invalidation is copied fromhere |
Contents |
| pos | - | iterator to the position before which the new element will be inserted |
| value | - | element value to insert |
| first, last | - | the pair of iterators defining the sourcerange of elements to insert |
| ilist | - | initializer list to insert the values from |
| x | - | a value of any type that can be transparently compared with a key |
| s | - | adisambiguation tag indicating that the input sequence is sorted (with respect tocompare) and contains only unique elements |
| Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
| This section is incomplete Reason: cases 6,8-10 |
size() otherwise. Plus the cost of insertion intoc.The hinted insert(3-5)does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such asstd::vector::insert. This makes it possible to create generic inserters such asstd::inserter. One way to check success of a hinted insert is to comparesize() before and after.
#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 iteratorassert(*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 iteratorassert(*result_2.first==3);if(!result_2.second)std::cout<<"no insertion\n";}
Output:
insert doneno insertion
| constructs element in-place (public member function)[edit] | |
| constructs elements in-place using a hint (public member function)[edit] | |
| creates astd::insert_iterator of type inferred from the argument (function template)[edit] |