In C++, std::list::insert() adds new elements before a position that you give as an iterator. The new nodes are linked in, and no existing element is copied or moved.
This is the strength of a list: inserting in the middle is quick once you hold the iterator. You can insert one value, several copies, a range, or a braced list. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call insert with an iterator that says where the new elements go, then the thing to insert.
C++
auto it = l.insert(pos, 20); // one value
l.insert(pos, 3, 0); // three copies of 0
l.insert(pos, src.begin(), src.end()); // a range
l.insert(pos, {7, 8}); // a braced list
Call
What it does
l.insert(pos, value)
Inserts one copy of value before pos. Returns an iterator to the new element.
l.insert(pos, count, value)
Inserts count copies of value before pos.
l.insert(pos, first, last)
Inserts a copy of every element of [first, last) before pos.
l.insert(pos, {7, 8})
Inserts the values of the braced list before pos.
To insert at the very end, pass l.end() as the position. To add at the ends without an iterator, push_back() and push_front() are shorter.
Required. An iterator into this list. The new elements are inserted before it. l.end() is allowed.
value
The value to insert, for the one-value form and the count form.
count
How many copies of value to insert.
first, last
The range to copy from. It must not point into this same list.
ilist
A braced list of values, such as {7, 8}.
Return Value
Return Value
Description
iterator
An iterator to the first element that was inserted. If nothing was inserted (count 0 or an empty range), it returns pos.
C++ Examples
Example 1 — insert() with One Value
Put 20 between 10 and 30, and use the iterator that comes back.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {10, 30};
auto it = l.insert(std::next(l.begin()), 20);
std::cout << "inserted: " << *it << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
inserted: 20
10 20 30
How It Works
std::next(l.begin()) moves one step forward, so it points at 30.
insert() links a new node holding 20before that position and returns an iterator to the new node.
Dereferencing the returned iterator with *it prints 20. The loop then shows the final order 10 20 30.
Example 2 — insert() with a Count and Value
Insert the same value several times with one call.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 5};
l.insert(std::next(l.begin()), 3, 0);
for (int n : l) {
std::cout << n << ' ';
}
std::cout << "(size " << l.size() << ")\n";
return 0;
}
Output
1 0 0 0 5 (size 5)
How It Works
The position is the node that holds 5, so the new items go right before it.
insert(pos, 3, 0) links three new nodes, each with the value 0.
The list grows from two to five elements. The old 1 and 5 were not touched.
Example 3 — insert() Keeps Your Iterators Valid
Keep an iterator to one element, insert a braced list at the front, and use the old iterator again.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 2, 3};
auto keep = std::next(l.begin()); // points at 2
l.insert(l.begin(), {7, 8});
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "keep still points at: " << *keep << '\n';
return 0;
}
Output
7 8 1 2 3
keep still points at: 2
How It Works
keep is an iterator that points at the node holding 2.
insert(l.begin(), {7, 8}) links two new nodes in front of 1. Only the links around the front change.
The node with 2 never moved, so *keep is still 2. In a vector, the same insert could invalidate keep.
Frequently Asked Questions
It adds one or more new elements before the position given by an iterator, and returns an iterator to the first new element.
Before the element that pos points to. So inserting at l.begin() adds a new first element, and inserting at l.end() adds a new last element.
Because a list only has to create a node and change the links of two neighbours. No other element is copied or moved. You still need an iterator to the position, and getting it can take a walk.
No. All existing iterators, pointers, and references stay valid after insert() on a std::list. This is a big difference from std::vector.
No. The first argument must be an iterator. Use l.insert(std::next(l.begin(), 2), 5) to insert before the third element.