std::inserter in C++ - GeeksforGeeks

27 Jul, 2017
std::inserter constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it. It is defined inside the header file .
An 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 a specific position in the container.Syntax:
std::inserter(Container& x, typename Container::iterator it);
x: Container in which new elements will 
be inserted.
it: Iterator pointing to the insertion point.

Returns: An insert_iterator that inserts elements into 
x at the position indicated by it.

// C++ program to demonstrate std::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 };      deque<int>::iterator i1;    i1 = v2.begin() + 1;    // i1 points to next element of 4 in v2      // Using std::inserter inside std::copy    std::copy(v1.begin(), v1.end(), std::inserter(v2, i1));    // v2 now contains 4 1 2 3 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 = 4 1 2 3 5 6 

How is it helpful ?
Inserting values anywhere : Now, just imagine, if we had to copy value into a container such as a vector, firstly, we had to move elements and then copy, but with the help of std::insert() we can insert at any position with ease.// C++ program to demonstrate std::inserter#include <iostream>#include <iterator>#include <vector>#include <algorithm>using namespace std;int main(){    // Declaring first container    vector<int> v1 = { 1, 2, 3, 7, 8, 9 };      // Declaring second container    vector<int> v2 = { 4, 5, 6 };      vector<int>::iterator i1;    i1 = v2.begin() + 2;    // i1 points to next element of 5 in v2      // Using std::inserter inside std::copy    std::copy(v1.begin(), v1.end(), std::inserter(v2, i1));    // v2 now contains 4 5 1 2 3 7 8 9 6      // Displaying v1 and v2    cout << "v1 = ";      int i;    for (i = 0; i < 6; ++i) {        cout << v1[i] << " ";    }      cout << "\nv2 = ";    for (i = 0; i < 9; ++i) {        cout << v2[i] << " ";    }      return 0;}Output:v1 = 1 2 3 7 8 9
v2 = 4 5 1 2 3 7 8 9 6
Explanation: Here, we started copying v1 into v2 but not from the beginning, but after the second position of v2, i.e., after 5, so all the elements of v1 were inserted after 5, and before 6. In this way, we inserted value where we wanted quite easily.
// C++ program to demonstrate std::inserter#include <iostream>#include <iterator>#include <vector>#include <algorithm>using namespace std;int main(){    // Declaring first container    vector<int> v1 = { 1, 2, 3, 7, 8, 9 };      // Declaring second container    vector<int> v2 = { 4, 5, 6 };      vector<int>::iterator i1;    i1 = v2.begin() + 2;    // i1 points to next element of 5 in v2      // Using std::inserter inside std::copy    std::copy(v1.begin(), v1.end(), std::inserter(v2, i1));    // v2 now contains 4 5 1 2 3 7 8 9 6      // Displaying v1 and v2    cout << "v1 = ";      int i;    for (i = 0; i < 6; ++i) {        cout << v1[i] << " ";    }      cout << "\nv2 = ";    for (i = 0; i < 9; ++i) {        cout << v2[i] << " ";    }      return 0;}
Output:
v1 = 1 2 3 7 8 9
v2 = 4 5 1 2 3 7 8 9 6

Explanation: Here, we started copying v1 into v2 but not from the beginning, but after the second position of v2, i.e., after 5, so all the elements of v1 were inserted after 5, and before 6. In this way, we inserted value where we wanted quite easily.
Points to Remember:
One of the pitfalls of std::inserter is that it can be used with only those containers that have insert as one of its methods like in case of vector, list and deque, and so on.insert() vs std::inserter(): Now, you may be thinking that insert() and std::inserter() are similar, but they are not. When you have to pass an iterator in the algorithm, then you should use inserter() like in above case, while for normally inserting the values in the container, insert() can be used.In place of using std::inserter, we can create a insert_iterator and then use it, as eventually, std::inserter returns a insert_iterator only.// C++ program to demonstrate 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 };      deque<int>::iterator ii;    ii = v2.begin() + 1;    // ii points after 4 in v2      // Declaring a insert_iterator    std::insert_iterator<std::deque<int> > i1(v2, ii);      // Using the iterator in the copy()    std::copy(v1.begin(), v1.end(), i1);    // v2 now contains 4 1 2 3 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 = 4 1 2 3 5 6

One of the pitfalls of std::inserter is that it can be used with only those containers that have insert as one of its methods like in case of vector, list and deque, and so on.
insert() vs std::inserter(): Now, you may be thinking that insert() and std::inserter() are similar, but they are not. When you have to pass an iterator in the algorithm, then you should use inserter() like in above case, while for normally inserting the values in the container, insert() can be used.
In place of using std::inserter, we can create a insert_iterator and then use it, as eventually, std::inserter returns a insert_iterator only.// C++ program to demonstrate 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 };      deque<int>::iterator ii;    ii = v2.begin() + 1;    // ii points after 4 in v2      // Declaring a insert_iterator    std::insert_iterator<std::deque<int> > i1(v2, ii);      // Using the iterator in the copy()    std::copy(v1.begin(), v1.end(), i1);    // v2 now contains 4 1 2 3 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 = 4 1 2 3 5 6

// C++ program to demonstrate 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 };      deque<int>::iterator ii;    ii = v2.begin() + 1;    // ii points after 4 in v2      // Declaring a insert_iterator    std::insert_iterator<std::deque<int> > i1(v2, ii);      // Using the iterator in the copy()    std::copy(v1.begin(), v1.end(), i1);    // v2 now contains 4 1 2 3 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 = 4 1 2 3 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.
cpp-iterator
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Classes and Objects
Virtual Function in C++
Templates in C++ with Examples
Constructors in C++
Operator Overloading in C++
Socket Programming in C/C++
vector erase() and clear() in C++
Substring in C++
Copy Constructor in C++
Polymorphism in C++