std::deque<T,Allocator>::insert
(1) | ||
iterator insert( const_iterator pos, const T& value ); |
||
iterator insert( const_iterator pos, T&& value ); |
(2) | (since C++11) |
(3) | ||
iterator insert( const_iterator pos, size_type count, const T& value ); |
||
(4) | ||
template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last ); |
||
iterator insert( const_iterator pos, std::initializer_list<T> ilist ); |
(5) | (since C++11) |
Inserts elements at the specified location in the container.
[first, last)
before pos.
This overload has the same effect as overload (3) if |
(until C++11) |
This overload participates in overload resolution only if |
(since C++11) |
All iterators (including the end() iterator) are invalidated. References are invalidated too, unless pos == begin() or pos == end(), in which case they are not invalidated.
Parameters
pos | - | iterator before which the content will be inserted (pos may be the end() iterator) |
value | - | element value to insert |
count | - | number of elements to insert |
first, last | - | the range of elements to insert, cannot be iterators into container for which insert is called |
ilist | - | initializer list to insert the values from |
Type requirements | ||
-T must meet the requirements of CopyAssignable and CopyInsertable in order to use overload (1).
| ||
-T must meet the requirements of MoveAssignable and MoveInsertable in order to use overload (2).
| ||
-T must meet the requirements of CopyAssignable and CopyInsertable in order to use overload (3).
| ||
-T must meet the requirements of EmplaceConstructible in order to use overload (4,5).
| ||
-T must meet the requirements of Swappable, MoveAssignable, MoveConstructible and MoveInsertable in order to use overload (4,5). (since C++17)
|
Return value
Complexity
Exceptions
If an exception is thrown other than by
- the copy constructor of
T
,
|
(since C++11) |
- the copy assignment operator of
T
,
|
(since C++11) |
this function has no effect (strong exception guarantee).
If an exception is thrown when inserting a single element at either end, this function has no effect (strong exception guarantee). Otherwise, if an exception is thrown by the move constructor of a non-CopyInsertable |
(since C++11) |
Example
#include <iostream> #include <iterator> #include <deque> void print(int id, const std::deque<int>& container) { std::cout << id << ". "; for (const int x: container) std::cout << x << ' '; std::cout << '\n'; } int main () { std::deque<int> c1(3, 100); print(1, c1); auto it = c1.begin(); it = c1.insert(it, 200); print(2, c1); c1.insert(it, 2, 300); print(3, c1); // reset `it` to the begin: it = c1.begin(); std::deque<int> c2(2, 400); c1.insert(std::next(it, 2), c2.begin(), c2.end()); print(4, c1); int arr[] = {501, 502, 503}; c1.insert(c1.begin(), arr, arr + std::size(arr)); print(5, c1); c1.insert(c1.end(), {601, 602, 603}); print(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
Defect reports
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 |
LWG 247 | C++98 | the complexity was only specified for inserting a single element |
also specified for inserting multiple elements |
See also
(C++11) |
constructs element in-place (public member function) |
inserts an element to the beginning (public member function) | |
adds an element to the end (public member function) | |
creates a std::insert_iterator of type inferred from the argument (function template) |