Updated March 29, 2023
Introduction to C++ Back_Inserter
The back_inserter method in C++ is used to construct an iterator, which holds the responsibility of inserting new elements to the end of the “x” or the container, with which the method is applied, and this method is defined within the header file of the program. In C++, this is a special type of output iterator designed to let the algorithms overwrite any elements and not make it mandatory to insert new elements.
Syntax
Now that we have understood what this C++ back_inserter method is let us learn the syntax and understand it. The syntax for back_inserter is as follows:
std::back_inserter ( Container & x );
When we have to insert the new elements at the end of the container, we pass the container within the parameters, and that is the same container we see here in the syntax. So what this method returns is the list of elements that are inserted at the end of the container. Moving on, we will now have a look at how the method explained works.
How does Back_Inserter Method work in C++?
Understanding how the back_inserter method works is important, and the way it works is that it creates a back-insert iterator, which is responsible for adding or inserting new elements to the list. This back insert iterator is of a special kind which allows the algorithm to overwrite the elements. Moving on, we will now understand the examples and demonstrate the working of back_inserter.
Examples of C++ Back_Inserter
Different examples are mentioned below:
Example #1
Our first example is the simple working of back_inserter; here, we add elements to the end of the list. The code is as follows:
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::fill_n(std::back_inserter(v), 3, -1);
std::cout << "\n The output for the code is: ";
for (int n : v)
std::cout << n << ' ';
}
Code Explanation:
Simply start with importing important system files and then into our main code. Then we have our std, which is our namespace for the program, followed by defining the scope; we have a vector with integer data type and values of 1 to 10. We then our statement of back_inserter, with container passed as n, just like we learned with syntax and followed by x parameter.
Then we have our first output print statement, which simply prints a string statement, and the back_inserter result will follow it. Finally, we have our for the statement, where we pass our vector holding the numbers as n and then the next line if our final output statement prints the numbers from vector in a negative form. Refer to the below-attached screenshot for a proper understanding of the output.
Output:
As expected, the output starts with the print statement and is then followed by the list of numbers. These numbers at the end include the result of back_inserter, which are the numbers in negative, and now, moving on to the next example.
Example #2
Our second example deals with assigning size for the container. We have a container with 3 values, and then we create another container without any specific size. Let’s see how it works. The code is as follows:
Code:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v1 = { 7, 8, 9 };
vector<int> v2;
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
cout << "\n Elements of Container 1 are : ";
int i;
for (i = 0; i < 3; ++i) {
cout << v1[i] << " ";
}
cout << "\n Elements of Container 2 are : ";
for (i = 0; i < 5; ++i) {
cout << v2[i] << " ";
}
return 0;
}
Code Explanation:
Started with all the system files needed, followed by initializing main. Then we have our first vector with an integer data type, and it holds 3 values, and the second vector of the same type, but with no specific size or values. Then begins our copy operation, where we are copying the begin and end part of vector 1 and implementing the back_inserter for vector 2.
Now we start printing the list of values that our vectors hold. First, a cout statement followed by the for a statement where we count and print each element of the vector. For our first vector, this for statement will only print 3 values, no more than that. Moving to our second vector, within for, we want it to print 5 values. Here we will have 2 new values, which will be zeros but added to the last part of the vector. Refer to the below-attached screenshot for a proper understanding of the output.
Output:
As expected, we have two print statements with values of 2 vectors.
Example #3
Moving on to our next example, we will see our third example. Let’s see how it works. The code is as follows:
Code:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main () {
std::vector<int> dab,bar;
for (int i=1; i<=5; i++){
dab.push_back(i); bar.push_back(i*5); }
std::copy (bar.begin(),bar.end(),back_inserter(dab));
std::cout << "\n Our Container contains: ";
for ( std::vector<int>::iterator it = dab.begin(); it!= dab.end(); ++it )
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Code Explanation:
Similar to our earlier example, we have system files followed by declaring two vectors of integer type. Then we have our for the statement, to add new value to the list and next statement we have push_back and us multiple our value from the list. These things happen within our first vector, from which we later copy all values into our second vector. Then comes our print statements and for statement for properly printing the values. Refer to the below attached screenshot.
Output:
Advantages
One of the major advantages is when we are not aware of the container size. So, when we have a situation where we have to insert new elements but are unaware of the size and how many new elements can be inserted, our operation cannot be successful. But with back_inserter, there is no need to declare any size of the container, meaning the container can be extended to any limit later on.
Along with these advantages, we need to understand that the back_inserter can only be employed with the containers that already have methods with push_back.
Conclusion
The back_inserter provides a method in C++ Programming Language that constructs an iterator and does the operation of inserting new elements to a list through the end. Using back_inserter has the advantage that we don’t need to have a proper number of the list. Moreover, we understood the implementation along with examples and codes.
Recommended Articles
This is a guide to C++ Back_Inserter. Here we discuss How Back_Inserter Method works in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –