Open In App

std::front_inserter in C++

std::front_inserter constructs a front-insert iterator that inserts new elements at the front of the container to which it is applied. It is defined inside the header file .

A front-insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements automatically at the beginning of the container. It is the exact opposite of std::back_inserter.



Syntax:

std::front_inserter (Container& x);

x: Container in which new elements will 
be inserted at the beginning.

Returns: A front_insert_iterator that inserts 
elements at the beginning of container x.




// C++ program to demonstrate std::front_inserter
#include <iostream>
#include <iterator>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
    // Declaring first container
    deque<int> v1 = { 1, 2, 3 };
  
    // Declaring second container for
    // copying values
    deque<int> v2 = { 4, 5, 6 };
  
    // Using std::front_inserter inside std::copy
    std::copy(v1.begin(), v1.end(), std::front_inserter(v2));
    // v2 now contains 3 2 1 4 5 6
  
    // Displaying v1 and v2
    cout << "v1 = ";
  
    int i;
    for (i = 0; i < 3; ++i) {
        cout << v1[i] << " ";
    }
  
    cout << "\nv2 = ";
    for (i = 0; i < 6; ++i) {
        cout << v2[i] << " ";
    }
  
    return 0;
}

Output:



v1 = 1 2 3
v2 = 3 2 1 4 5 6 

How is it helpful ?

Points to Remember:

  1. One of the pitfalls of std::front_inserter is that it can be used with only those containers that have push_front as one of its methods like in case of list and deque, and it cannot be used with vectors.
  2. push_front() vs front_inserter(): Now, you may be thinking that push_front() and front_inserter are similar, but they are not. When you have to pass an iterator in the algorithm, then you should use front_inserter like in above case, while for normally inserting the values in the beginning of the container, push_front() can be used.
  3. In place of using std::front_inserter, we can create a front_insert_iterator and then use it, as eventually, std::front_inserter returns a front_insert_iterator only.




    // C++ program to demonstrate front_insert_iterator
    #include <iostream>
    #include <iterator>
    #include <deque>
    #include <algorithm>
    using namespace std;
    int main()
    {
        // Declaring first container
        deque<int> v1 = { 1, 2, 3 };
      
        // Declaring second container for
        // copying values
        deque<int> v2 = { 4, 5, 6 };
      
        // Declaring a front_insert_iterator
        std::front_insert_iterator<std::deque<int> > front_i1(v2);
      
        // Using the iterator in the copy()
        std::copy(v1.begin(), v1.end(), front_i1);
        // v2 now contains 3 2 1 4 5 6
      
        // Displaying v1 and v2
        cout << "v1 = ";
      
        int i;
        for (i = 0; i < 3; ++i) {
            cout << v1[i] << " ";
        }
      
        cout << "\nv2 = ";
        for (i = 0; i < 6; ++i) {
            cout << v2[i] << " ";
        }
      
        return 0;
    }
    
    

    Output:

    v1 = 1 2 3
    v2 = 3 2 1 4 5 6
    

Article Tags :