Std::Back_inserter in C++
The C++ Standard Library function std::back_inserter is a helpful tool for inserting elements into containers. These containers can be std::vectors, std::deques, or any other container that can do the push_back action. It is a component of the <iterator> header and is frequently used with the std::copy, std::transform, and std::for_each Standard Template Library (STL) algorithms.
The main goal of std::back_inserter is to make it easier to add components to the end of a container without requiring manual memory allocation and size management of the container. When combined with algorithms, it generates a unique iterator that inserts elements automatically by using the container's push_back function. It is helpful for filling a container during data processing or modification operations.
Syntax:
It has the following syntax:
std::back_inserter (Container& x);
x: It is the container in which new elements will be inserted at the end.
Returns: A back_insert_iterator that inserts elements at the end of container x.
C++ Code Using Std::Back_inserter:
Let's take an example to illustrate the use of std::back_inserter in C++:
#include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { std::vector<int> numbers; std::vector<int> source = {3, 6, 9, 12, 15}; // Use std::back_inserter to insert elements into the 'numbers' vector. std::copy(source.begin(), source.end(), std::back_inserter(numbers)); for (const auto& num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output:
3 6 9 12 15
Explanation:
In this example, use std::back_inserter(numbers) to create a back-insert iterator for the numbers vector. It is not necessary to explicitly enlarge the numbers vector when using std::copy with this iterator to append the elements from the source vector to the end of the numbers vector. It is also a useful approach to work with dynamic data without having to worry about container management. It makes developing or extending containers easier.
It should be noted that std::back_inserter is often used with containers that permit push_back; it is not suitable for use with, instance, std::set or std::map. For those containers, one would use Std::inserter or the equivalent insert iterator.
Example 2:
// C++ program to demonstrate std::back_inserter #include <iostream> #include <iterator> #include <vector> #include <algorithm> using namespace std; int main() { // Declaring first container vector<int> v1 = { 1, 2, 3 }; // Declaring second container for // copying values vector<int> v2 = { 4, 5, 6 }; // Using std::back_inserter inside std::copy std::copy(v1.begin(), v1.end(), std::back_inserter(v2)); // v2 now contains 4 5 6 1 2 3 // 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 5 6 1 2 3
Example 3:
// C++ program to demonstrate std::back_inserter #include <iostream> #include <iterator> #include <vector> #include <algorithm> using namespace std; int main() { // Declaring first container vector<int> v1 = { 9, 18, 27 }; // Declaring second container without specifying its size vector<int> v2; // Using std::back_inserter inside std::copy std::copy(v1.begin(), v1.end(), std::back_inserter(v2)); // v2 now contains 1 2 3 // 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 = 9 18 27 v2 = 9 18 27
Explanation:
In this example, we needed to copy v1 into v2, but let's imagine we are in a situation where we are unsure of the number of pieces that would be transferred into v2, so we won't give its size and will instead copy into it using std::back_inserter().
Conclusion:
For easily adding items into containers that implement the push_back function, the C++ Standard Library offers a useful utility called std::back_inserter. It removes the need for manual memory allocation and size management and streamlines the process of appending pieces to the end of a container. You can use std::back_inserter in conjunction with different Standard Template Library (STL) methods to efficiently fill containers during data processing or transformations. With this iterator, you may write code that is more comprehensible, readable, and less vulnerable to error while working with dynamic data, constructing, or extending containers. It is a useful tool for any C++ code when working with standard containers such as vectors and deques.