(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 |
| Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Deduction guides(C++17) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iterator insert( const_iterator pos,const T& value); | (1) | (constexpr since C++26) |
iterator insert( const_iterator pos, T&& value); | (2) | (since C++11) (constexpr since C++26) |
iterator insert( const_iterator pos, size_type count,const T& value); | (3) | (constexpr since C++26) |
template<class InputIt> iterator insert( const_iterator pos, InputIt first, InputIt last); | (4) | (constexpr since C++26) |
iterator insert( const_iterator pos,std::initializer_list<T> ilist); | (5) | (since C++11) (constexpr since C++26) |
Inserts elements at the specified location in the container.
If | (since C++11) |
| (since C++11) |
T is notCopyAssignable.[first, last) beforepos.This overload has the same effect as overload(3) if | (until C++11) |
This overload participates in overload resolution only if | (since C++11) |
| (since C++11) |
No iterators or references are invalidated.
Contents |
| pos | - | iterator before which the content will be inserted |
| value | - | element value to insert |
| count | - | number of elements to insert |
| first, last | - | the pair of iterators defining therange of elements to insert |
| ilist | - | std::initializer_list to insert the values from |
Linear in the number of elements inserted.
If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).
#include <iostream>#include <iterator>#include <string_view>#include <list> namespace stq{void println(std::string_view rem,conststd::list<int>& container){std::cout<< rem.substr(0, rem.size()-2)<<'[';bool first{true};for(constint x: container)std::cout<<(first? first=false,"":", ")<< x;std::cout<<"]\n";}} int main(){std::list<int> c1(3,100); stq::println("1. {}", c1); auto pos= c1.begin(); pos= c1.insert(pos,200);// overload (1) stq::println("2. {}", c1); c1.insert(pos,2,300);// overload (3) stq::println("3. {}", c1); // reset pos to the begin: pos= c1.begin(); std::list<int> c2(2,400); c1.insert(std::next(pos,2), c2.begin(), c2.end());// overload (4) stq::println("4. {}", c1); int arr[]={501,502,503}; c1.insert(c1.begin(), arr, arr+std::size(arr));// overload (4) stq::println("5. {}", c1); c1.insert(c1.end(),{601,602,603});// overload (5) stq::println("6. {}", c1);}
Output:
1. [100, 100, 100]2. [200, 100, 100, 100]3. [300, 300, 200, 100, 100, 100]4. [300, 300, 400, 400, 200, 100, 100, 100]5. [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100]6. [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100, 601, 602, 603]
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 149 | C++98 | overloads(3) and(4) returned nothing | returns an iterator |
(C++11) | constructs element in-place (public member function)[edit] |
| inserts an element to the beginning (public member function)[edit] | |
| adds an element to the end (public member function)[edit] | |
| creates astd::insert_iterator of type inferred from the argument (function template)[edit] |