In C++, std::list<T> is a container that holds a row of elements of type T. It is a doubly linked list. Each element sits in its own small node, and every node keeps a link to the previous node and to the next node. You can walk the list forwards and backwards.
Because the elements are linked, you can insert or erase anywhere quickly once you have an iterator, and no other element moves. The price is that a list has no access by index: l[2] and l.at(2) do not exist. Common functions are push_back(), push_front(), front(), and back(). Include <list> to use it.
This page teaches what a std::list is. The List Functions index lists every function guide in this series.
Why choose a list?
Choose a list when you insert, erase, or move elements in the middle often, and you keep iterators to them. It also has handy member functions such as splice(), merge(), and sort(). If you mostly read elements by position or loop through all of them, a std::vector is usually the better and faster choice.
Syntax
C++ Syntax
Common ways to create and use a std::list:
C++
#include <iostream>
#include <list>
std::list<int> a; // empty list
std::list<int> b = {3, 1, 2}; // three elements
std::list<int> c(5, 0); // five zeros
b.push_front(0); // 0 3 1 2
b.push_back(4); // 0 3 1 2 4
int first = b.front(); // 0
int last = b.back(); // 4
b.sort(); // 0 1 2 3 4
b.reverse(); // 4 3 2 1 0
b.pop_front(); // 3 2 1 0
b.pop_back(); // 3 2 1
Reference
std::list vs std::vector vs std::deque
The three containers look alike but behave differently. Here is a quick comparison:
Feature
std::vector
std::deque
std::list
Access by index x[i]
Fast
Fast
Not available
Add / remove at the back
Fast
Fast
Fast
Add / remove at the front
Slow (no push_front())
Fast
Fast
Insert / erase in the middle
Slow (elements shift)
Slow (elements shift)
Fast (with an iterator)
Iterators after insert
May become invalid
May become invalid
Stay valid
Move nodes between containers
Copy or move each element
Copy or move each element
splice() relinks nodes
Memory layout
One block
Several blocks
Separate nodes
Guide
When to Use the List Functions
A list has a few member functions that no other sequence container has. Use these instead of the same-named functions from <algorithm>:
std::list<int> l = {20, 30} starts with two nodes.
push_front(10) links a new node before them, and push_back(40) links one after them. The list is now 10 20 30 40.
front() reads the first element, back() reads the last one, and size() counts all four.
Example 2 — Insert and Erase in the Middle with Iterators
A list has no l[i]. You move an iterator with std::next(), then insert or erase at that spot without shifting any other element.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {10, 20, 40, 50};
auto it = std::next(l.begin(), 2); // points at 40
l.insert(it, 30); // 10 20 30 40 50
it = std::next(l.begin(), 1); // points at 20
l.erase(it); // 10 30 40 50
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
10 30 40 50
How It Works
std::next(l.begin(), 2) walks two nodes forward and gives an iterator to 40. A list cannot jump there, so it really walks.
insert(it, 30) links a new node just before 40. No other element moves or is copied.
erase() on the iterator to 20 unlinks that node. The loop then prints 10 30 40 50.
Example 3 — Use the Special List Functions: sort(), unique() and splice()
These three member functions are why people pick a list. They rearrange nodes instead of copying values.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> a = {4, 1, 4, 3, 1};
std::list<int> b = {100, 200};
a.sort(); // 1 1 3 4 4
a.unique(); // 1 3 4
a.splice(a.end(), b); // move all of b to the end of a
for (int n : a) {
std::cout << n << ' ';
}
std::cout << "| b has " << b.size() << " elements\n";
return 0;
}
Output
1 3 4 100 200 | b has 0 elements
How It Works
sort() puts the list in order: 1 1 3 4 4. unique() only removes neighbouring repeats, so sorting first makes it remove all duplicates and leaves 1 3 4.
splice(a.end(), b) moves every node of b to the end of a. The values are not copied, only the links change.
After the splice, a holds five elements and b is empty, because its nodes now belong to a.
Continue
Next: C++ std::list Functions Index
After you know what a std::list is, open the function index for a full tutorial on each member function.
std::list<T> is a container from <list>. It is a doubly linked list: every element is stored in its own node, and each node knows the previous and the next node. It has no access by index.
A vector keeps elements in one block, allows v[i], and is fast at the back. A list keeps separate nodes, has no [], but can insert, erase, and splice in the middle quickly once you have an iterator. A vector is usually faster to loop through.
A deque allows d[i] and is fast at both ends, but inserting in the middle moves elements. A list has no [], but inserting or erasing at an iterator never moves other elements, and iterators to other elements stay valid.
std::sort() needs random access iterators, and a list only has bidirectional iterators. So the list provides a member sort() that relinks nodes. It is stable, which means equal elements keep their order.
Open the C++ std::list Functions index. It lists every member function, from push_back() and splice() to sort() and unique(), each with its own tutorial.