C++ std::list Functions

About This Index
This page is the C++ std::list Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::list is on /cpp/list. Use this page when you need a specific member function such as splice(), sort(), or remove().
A std::list<T> inserts and erases quickly anywhere once you hold an iterator, and it has its own member sort(), merge(), splice(), remove(), unique(), and reverse(). It has no [], at(), capacity(), or reserve(). The 37 functions are listed below.
How to Call a List Function
Include <list>, create the list, and call members with a dot:
#include <list>
#include <iostream>
std::list<int> l = {30, 10, 20};
l.push_front(5); // 5 30 10 20
l.push_back(40); // 5 30 10 20 40
int first = l.front(); // 5
int last = l.back(); // 40
l.sort(); // 5 10 20 30 40
l.pop_back(); // 5 10 20 30
std::size_t n = l.size(); // 4 C++ std::list Functions Table
All function guides in this C++ tutorial series (37 tutorials), grouped by purpose.
Element access
2 functionsRead the first or last element. A list has no [] or at(), because it has no access by index.
Capacity
4 functionsAsk about the size, or change it. A list has no capacity() or reserve().
Modifiers
14 functionsAdd, remove, or replace elements. Both ends are fast, and so is the middle once you hold an iterator.
| Function | Description | Tutorial |
|---|---|---|
assign() | Replace all elements with new ones. | Try it |
assign_range() | Replace all elements with the items of a range (C++23). | Try it |
clear() | Erase every element from the list. | Try it |
insert() | Add one or more elements before an iterator. | Try it |
insert_range() | Insert the items of a range before an iterator (C++23). | Try it |
erase() | Remove one element or a range of elements by iterator. | Try it |
push_back() | Add an element at the end of the list. | Try it |
push_front() | Add an element at the start of the list. | Try it |
pop_back() | Remove the last element of the list. | Try it |
pop_front() | Remove the first element of the list. | Try it |
emplace() | Build a new element in place before an iterator (C++11). | Try it |
emplace_back() | Build a new last element in place (C++11). | Try it |
emplace_front() | Build a new first element in place (C++11). | Try it |
swap() | Exchange the contents of two lists. | Try it |
List operations
8 functionsSpecial member functions that work on the whole list: merge, move nodes, remove by value, reverse, sort, and drop duplicates.
| Function | Description | Tutorial |
|---|---|---|
merge() | Merge another sorted list into this sorted list. | Try it |
splice() | Move nodes from another list into this list without copying. | Try it |
splice_range() | Move a range of nodes from one list into another. | Try it |
remove() | Erase every element equal to a value. | Try it |
remove_if() | Erase every element for which a test returns true. | Try it |
reverse() | Reverse the order of the elements. | Try it |
sort() | Sort the elements of the list (stable). | Try it |
unique() | Erase repeated neighbouring elements. | Try it |
Iterators
8 functionsWalk through the list forwards or backwards. The c versions are read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Get an iterator to the first element. | Try it |
end() | Get an iterator one place after the last element. | Try it |
cbegin() | Get a read-only iterator to the first element (C++11). | Try it |
cend() | Get a read-only iterator one place after the last element (C++11). | Try it |
rbegin() | Get a reverse iterator to the last element. | Try it |
rend() | Get a reverse iterator one place before the first element. | Try it |
crbegin() | Get a read-only reverse iterator to the last element (C++11). | Try it |
crend() | Get a read-only reverse iterator one place before the first element (C++11). | Try it |
Allocator
1 functionAccess the memory helper object used by the list.
| Function | Description | Tutorial |
|---|---|---|
get_allocator() | Get a copy of the allocator the list uses. | Try it |
Frequently Asked Questions
Start with push_back()
Add an element at the end of a list, the first thing most programs do.
12 people found this page helpful
