C++ Standard Library C++ STL Library

C++ <vector> - insert() Function



The C++ vector::insert function is used to insert new elements before the element at the specified position. This results into increasing the vector size by the number of elements inserted. Reallocation of storage happens when the new vector size surpasses the current vector capacity.

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 vector where the new elements need to be inserted.
val Specify the value to be copied (or moved) to the inserted elements.
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 vector::insert function is used to insert elements in the given vector.

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> vec1 = {10, 20, 30};
  vector<int> vec2 = {10, 20, 30};
  vector<int> vec3 = {10, 20, 30};
  vector<int> vec4 = {100, 200, 300};

  vector<int>::iterator it;

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

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

  //range version 
  it = vec3.begin();
  vec3.insert(it+2, vec4.begin(), vec4.end());

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

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

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

  return 0;
}

The output of the above code will be:

vec1 contains: 10 20 55 30 
vec2 contains: 10 20 55 55 55 30 
vec3 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 vector into the another vector. Consider the example below.

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

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

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

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

  return 0;
}

The output of the above code will be:

vec1 contains: 10 20 30 
vec2 contains: 10 20 30 100 200 300 

Example:

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

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

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

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

  return 0;
}

The output of the above code will be:

vec contains: 10 20 11 22 33 44 55 30 

❮ C++ <vector> Library