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 ?
-
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.
// 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;
}
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:
- 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.
- 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.
-
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