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.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

How is it helpful ?

  • Reversing a container: Now, since std::front_inserter inserts new elements at the beginning of the container, so we can perform the task of reverse_copy() just with the help of copy(), such that we will create another container which contains the reverse of present container.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 storing the reverse
        deque<int> v2;
      
        // Using std::front_inserter inside std::copy
        std::copy(v1.begin(), v1.end(), std::front_inserter(v2));
        // v2 now contains 3 2 1
      
        // Displaying v1 and v2
        cout << "v1 = ";
      
        int i;
        for (i = 0; i < 3; ++i) {
            cout << v1[i] << " ";
        }
      
        cout << "\nv2 = ";
        for (i = 0; i < 3; ++i) {
            cout << v2[i] << " ";
        }
      
        return 0;
    }

    chevron_right

    
    

    Output:

    v1 = 1 2 3
    v2 = 3 2 1
    

    Explanation: Here, we started copying v1 into v2 but from the beginning, so every time a new element came it was inserted in the beginning, in this way, the last element inserted became the first element in the new container, and in this way we were able to reverse the contents of the container.

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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    Output:

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

This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.