std::back_inserter() is a utility function in the C++ Standard Library that creates a back-insert iterator for a container. Instead of overwriting existing elements, it automatically inserts new elements at the end of the container using push_back().
- Creates an output iterator that inserts elements at the end of a container.
- Commonly used with STL algorithms such as std::copy() and std::transform().
Example: Using std::back_inserter()
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {4, 5, 6};
copy(v1.begin(), v1.end(), back_inserter(v2));
for (auto x : v2)
cout << x << " ";
return 0;
}
Output
4 5 6 1 2 3
Explanation: std::back_inserter(v2) creates an output iterator that appends elements to v2 using push_back().
Syntax
std::back_inserter(Container& x);
Parameters: x - Container in which elements will be inserted at the end.
Return Value: Returns a std::back_insert_iterator associated with the container.
Working of std::back_inserter()
std::back_inserter() creates a special output iterator that inserts elements at the end of a container using the container's push_back() function.
- Creates a back_insert_iterator associated with the specified container.
- Every assignment to the iterator automatically calls push_back().
- Allows STL algorithms to append elements without requiring preallocated space.
Internally, the following operation:
*iterator = value;
is equivalent to:
container.push_back(value);
Example 1: Copying Elements into an Existing Container
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {4, 5, 6};
copy(v1.begin(), v1.end(),
back_inserter(v2));
for (auto x : v2)
cout << x << " ";
return 0;
}
Output
4 5 6 1 2 3
Example 2: Copying into an Empty Container
One of the biggest advantages of std::back_inserter() is that the destination container does not need to have a predefined size.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> source = {1, 2, 3};
vector<int> destination;
copy(source.begin(),
source.end(),
back_inserter(destination));
for (auto x : destination)
cout << x << " ";
return 0;
}
Output
1 2 3
Explanation: Since destination is initially empty, std::back_inserter() automatically inserts elements using push_back().
Containers Supported by std::back_inserter()
std::back_inserter() works only with containers that support the push_back() member function. Examples include:
- std::vector
- std::deque
- std::list
- std::string
It cannot be used with containers such as:
- std::set
- std::map
- std::array
- std::forward_list
Advantages of std::back_inserter()
std::back_inserter() provides a convenient way to insert elements into a container while using STL algorithms.
- Automatically appends elements to the end of a container.
- Eliminates the need to preallocate the container size.
- Allows algorithms such as std::copy() to work with initially empty containers.
- Improves code readability by avoiding explicit push_back() calls.
push_back() vs std::back_inserter()
| push_back() | std::back_inserter() |
|---|---|
| Inserts a single element | Creates an output iterator |
| Used directly with containers | Used with STL algorithms |
| Requires explicit calls | Performs insertion automatically |
| Example: v.push_back(10) | Example: copy(..., back_inserter(v)) |
Note: std::back_inserter() internally returns a std::back_insert_iterator object.