std::deque insert() method
- since C++11
- until C++11
// (1) Non const version only
iterator insert( const_iterator pos, const T& value );
// (2) Non const version only
iterator insert( const_iterator pos, T&& value );
// (3) Non const version only
iterator insert( const_iterator pos, size_type count, const T& value );
// (4) Non const version only
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
// (5) Non const version only
iterator insert( const_iterator pos, std::initializer_list<T> ilist );
// (1) Non const version only
iterator insert( iterator pos, const T& value );
// (3) Non const version only
void insert( iterator pos, size_type count, const T& value );
// (4) Non const version only
template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last );
Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
-
(1-2) Inserts
valuebeforepos. -
(3) Inserts
countcopies of the value beforepos. -
(4) Inserts elements from range [ first; last ) before
pos.- since C++11
- until C++11
This overload participates in overload resolution only if
InputItqualifies asLegacyInputIterator, to avoid ambiguity with the overload (3).This overload has the same effect as overload (3) if
InputItis an integral type.Undefined behaviourThe behavior is undefined if
firstandlastare iterators into*this. -
(5) Inserts elements from initializer list
ilistbeforepos.Undefined behaviourAll iterators, including the past-the-end iterator, are invalidated. References are invalidated too, unless
pos == begin()orpos == end(), in which case they are not invalidated.
Parameters
pos- iterator before which the content will be inserted. pos may be theend()iteratorvalue- element value to insertcount- number of elemnts to insertfirst,last- range of elements to insertilist- initializer list to insert the values from
Type requirements
- (1) -
Tmust meet the requirements ofCopyAssignableandCopyInsertable. - (2) -
Tmust meet the requirements ofMoveAssignableandMoveInsertable. - (3) -
Tmust meet the requirements ofCopyAssignableandCopyInsertable. - (4-5) -
Tmust meet the requirements ofEmplaceConstructible. -
- since C++17
(4-5) -
Tmust meet the requirements ofSwappable,MoveAssignable,MoveConstructibleandMoveInsertable.
Return value
- (1-2) Iterator pointing to the inserted value.
- (3) Iterator pointing to the first element inserted, or
posifcount == 0. - (4) Iterator pointing to the first element inserted, or
posiffirst == last. - (5) Iterator pointing to the first element inserted, or
posifilistis empty.
Complexity
For all of the below N is std::min(std::distance(pos, begin()), std::distance(pos, end()))
- (1-2) Linear in the lesser of the distances between
posand either of the ends of the container - O(N). - (3) Linear in
count+ linear in the lesser of the distances betweenposand either of the ends of the container - O(count + N). - (4) Linear in
std::distance(first, last)+ linear in the lesser of the distances betweenposand either of the ends of the container - O(std::distance(first, last) + N). - (5) Linear in
ilist.size()+ linear in the lesser of the distances betweenposand either of the ends of the container - O(ilist.size() + N).
Exceptions
If an exception is thrown when inserting a single element at either end, this function has no effect (strong exception guarantee).
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);
}
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
Hover to see the original license.