std::inserter is a utility function in C++ STL that constructs an insert iterator, which inserts elements into a container at a specified position. It is defined in the <iterator> header and is commonly used with STL algorithms such as std::copy() and std::transform().
- Creates an insert iterator that inserts elements instead of overwriting them.
- Allows STL algorithms to insert elements into containers at a desired position.
Example: Using std::inserter with std::copy()
#include <iostream>
#include <iterator>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
deque<int> source = {1, 2, 3};
deque<int> destination = {4, 5, 6};
auto pos = destination.begin() + 1;
copy(source.begin(), source.end(), inserter(destination, pos));
for (auto x : destination)
cout << x << " ";
return 0;
}
Output
4 1 2 3 5 6
Explanation: The iterator pos points to the element 5. The std::copy() algorithm inserts all elements of source before this position, resulting in: 4 1 2 3 5 6
Syntax
std::inserter(Container& container,
typename Container::iterator pos);
Parameters
- container: The container in which elements will be inserted.
- pos: Iterator specifying the insertion position.
Return Value
Returns an object of type std::insert_iterator associated with the specified container.
Inserting Elements at Any Position
One of the biggest advantages of std::inserter is that it allows insertion at any valid position in a container.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<int> v1 = {1, 2, 3, 7, 8, 9};
vector<int> v2 = {4, 5, 6};
auto pos = v2.begin() + 2;
copy(v1.begin(), v1.end(), inserter(v2, pos));
for (auto x : v2)
cout << x << " ";
return 0;
}
Output
4 5 1 2 3 7 8 9 6
Explanation: The elements of v1 are inserted after the second element (5) of v2, without manually shifting elements.
Using std::insert_iterator Directly
The std::inserter() function internally creates an object of type std::insert_iterator. We can also create it explicitly.
#include <iostream>
#include <deque>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
deque<int> d1 = {1, 2, 3};
deque<int> d2 = {4, 5, 6};
auto pos = d2.begin() + 1;
insert_iterator<deque<int>> it(d2, pos);
copy(d1.begin(), d1.end(), it);
for (auto x : d2)
cout << x << " ";
return 0;
}
Output
4 1 2 3 5 6
std::insert_iterator
std::inserter() internally creates and returns an object of type std::insert_iterator. We can also create an insert_iterator explicitly.
#include <iostream>
#include <iterator>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
deque<int> v1 = {1, 2, 3};
deque<int> v2 = {4, 5, 6};
auto pos = v2.begin() + 1;
insert_iterator<deque<int>> itr(v2, pos);
copy(v1.begin(), v1.end(), itr);
cout << "v2 = ";
for (auto x : v2)
cout << x << " ";
return 0;
}
Output
v2 = 4 1 2 3 5 6
Advantages of Using std::inserter
std::inserter is useful when you want STL algorithms to insert elements into a container rather than overwrite existing ones.
- Allows STL algorithms to insert elements at a specified position in a container.
- Eliminates the need to manually shift existing elements before insertion.
- Works seamlessly with algorithms such as std::copy(), std::transform(), and std::merge().
- Makes inserting elements into the middle of a container simpler and more readable.
Limitations of std::inserter
std::inserter has some limitations:
- It can only be used with containers that provide an insert() member function.
- It may be less efficient for containers where insertion requires shifting elements, such as std::vector.
- It cannot be used with fixed-size containers such as std::array.
Containers Supported by std::inserter
The following STL containers support std::inserter:
- std::vector
- std::deque
- std::list
- std::set
- std::multiset
- std::map
- std::multimap
- std::unordered_set
- std::unordered_map
insert() vs std::inserter()
| insert() | std::inserter() |
|---|---|
| Used to insert elements directly into a container. | Used to create an insert iterator for STL algorithms. |
| Called explicitly by the programmer. | Usually passed as an argument to STL algorithms like copy(). |
| Inserts a single element or range. | Enables algorithms to insert elements automatically. |