C++ Standard Library C++ STL Library

C++ <forward_list> - insert_after() Function



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

Syntax

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

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

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

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

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

Parameters

position Specify the iterator position in the container 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 last element of the newly inserted elements, or position if no element is inserted.

Time Complexity

Linear i.e, Θ(n)

Example:

In the example below, the forward_list::insert_after function is used to insert elements in the given forward list.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist1 = {10, 20, 30};
  forward_list<int> flist2 = {10, 20, 30};
  forward_list<int> flist3 = {10, 20, 30};
  forward_list<int> flist4 = {100, 200, 300};

  forward_list<int>::iterator it;

  //single element version 
  it = flist1.begin();  it++;   //points to second element
  flist1.insert_after(it, 55);

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

  //range version 
  it = flist3.begin();
  flist3.insert_after(++it, flist4.begin(), flist4.end());

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

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

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

  return 0;
}

The output of the above code will be:

flist1 contains: 10 20 55 30 
flist2 contains: 10 20 55 55 55 30 
flist3 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 forward list into the another forward list. Consider the example below.

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

  //move version - moving content of flist1 to flist2 
  for(it = flist1.begin(); it != flist1.end(); ++it)
    flist2.insert_after(flist2.begin(), move(*it));

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

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

  return 0;
}

The output of the above code will be:

flist1 contains: 10 20 30 
flist2 contains: 100 30 20 10 200 300 

Example:

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

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

  //initializer list version 
  initializer_list<int> ilist = {11, 22, 33, 44, 55};
  it = flist.begin();
  flist.insert_after(++it, ilist); 

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

  return 0;
}

The output of the above code will be:

flist contains: 10 20 11 22 33 44 55 30 

❮ C++ <forward_list> Library