C++ Standard Library C++ STL Library

C++ <deque> - insert() Function



The C++ deque::insert function is used to insert new elements before the element at the specified position. This results into increasing the deque size by the number of elements inserted.

Syntax

//single element version  
iterator insert (iterator position, const value_type& val);

//fill version  
void insert (iterator position, size_type n, const value_type& val);

//range version 
template <class InputIterator>
  void insert (iterator position, InputIterator first, InputIterator last);
//single element version 
iterator insert (const_iterator position, const value_type& val);

//fill version  
iterator insert (const_iterator position, size_type n, const value_type& val);

//range version 
template <class InputIterator>
  iterator insert (const_iterator position, InputIterator first, InputIterator last);

//move version  
iterator insert (const_iterator position, value_type&& val);

//initializer list version 
iterator insert (const_iterator position, initializer_list<value_type> ilist);

Parameters

position Specify the iterator position in the deque where the new elements need to be inserted.
val Specify the value to be inserted.
n Specify the number of new elements to be inserted.
first Specify the starting position of InputIterator. Copies of the elements in the range [first,last) are inserted at position (in the same order).
last Specify the last position of InputIterator. Copies of the elements in the range [first,last) are inserted at position (in the same order).
ilist Specify the initializer_list object.

Return Value

Returns an iterator which points to the first element of the newly inserted elements.

Time Complexity

Linear i.e, Θ(n)

Example:

In the example below, the deque::insert function is used to insert elements in the given deque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> deque1 = {10, 20, 30};
  deque<int> deque2 = {10, 20, 30};
  deque<int> deque3 = {10, 20, 30};
  deque<int> deque4 = {100, 200, 300};

  deque<int>::iterator it;

  //single element version 
  it = deque1.begin();
  deque1.insert(it + 2, 55);

  //fill version - fill 3 new elements at specified location 
  it = deque2.begin();
  deque2.insert(it + 2, 3, 55);

  //range version 
  it = deque3.begin();
  deque3.insert(it+2, deque4.begin(), deque4.end());

  cout<<"deque1 contains: ";
  for(it = deque1.begin(); it != deque1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndeque2 contains: ";
  for(it = deque2.begin(); it != deque2.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndeque3 contains: ";
  for(it = deque3.begin(); it != deque3.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

deque1 contains: 10 20 55 30 
deque2 contains: 10 20 55 55 55 30 
deque3 contains: 10 20 100 200 300 30 

Example:

The move version of the insert function can also be used to insert the content of one deque into the another deque. Consider the example below.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> deque1 = {10, 20, 30};
  deque<int> deque2 = {100, 200, 300};
  deque<int>::iterator it;

  //move version - moving content of deque1 to deque2 
  for(int i = 0; i < deque1.size(); i++)
    deque2.insert(deque2.begin() + i, move(*(deque1.begin()+i)));

  cout<<"deque1 contains: ";
  for(it = deque1.begin(); it != deque1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndeque2 contains: ";
  for(it = deque2.begin(); it != deque2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

deque1 contains: 10 20 30 
deque2 contains: 10 20 30 100 200 300 

Example:

Similarly, the initializer list version can be used to insert elements in the given deque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> dq = {10, 20, 30};
  deque<int>::iterator it;

  //initializer list version 
  initializer_list<int> MyList = {11, 22, 33, 44, 55};
  it = dq.begin();
  dq.insert(it+2, MyList); 

  cout<<"dq contains: ";
  for(it = dq.begin(); it != dq.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

dq contains: 10 20 11 22 33 44 55 30 

❮ C++ <deque> Library