C++ std::list

Beginner
8 min read
Updated: Nov 2026
3 examples
std::list

Definition and Usage

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.

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

std::list vs std::vector vs std::deque

The three containers look alike but behave differently. Here is a quick comparison:

Featurestd::vectorstd::dequestd::list
Access by index x[i]FastFastNot available
Add / remove at the backFastFastFast
Add / remove at the frontSlow (no push_front())FastFast
Insert / erase in the middleSlow (elements shift)Slow (elements shift)Fast (with an iterator)
Iterators after insertMay become invalidMay become invalidStay valid
Move nodes between containersCopy or move each elementCopy or move each elementsplice() relinks nodes
Memory layoutOne blockSeveral blocksSeparate nodes

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>:

FunctionUse it when you want to…
sort()Put the list in order. std::sort() cannot be used on a list.
splice()Move elements from one list into another without copying them
remove() / remove_if()Erase elements by value or by a rule. The list version really erases, so no extra erase() is needed.
unique()Remove repeated neighbours (sort first to remove all duplicates)
merge()Combine two sorted lists into one sorted list
reverse()Flip the order of the elements

C++ Examples

Three short programs that show how std::list works.

Example 1 — Add at Both Ends with push_front() and push_back()

A first program that adds to the front and the back of a list, then reads both ends.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {20, 30};

    l.push_front(10);
    l.push_back(40);

    std::cout << "front: " << l.front() << '\n';
    std::cout << "back: " << l.back() << '\n';
    std::cout << "size: " << l.size() << '\n';
    return 0;
}

How It Works

  1. std::list<int> l = {20, 30} starts with two nodes.
  2. 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.
  3. 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;
}

How It Works

  1. std::next(l.begin(), 2) walks two nodes forward and gives an iterator to 40. A list cannot jump there, so it really walks.
  2. insert(it, 30) links a new node just before 40. No other element moves or is copied.
  3. 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;
}

How It Works

  1. 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.
  2. splice(a.end(), b) moves every node of b to the end of a. The values are not copied, only the links change.
  3. After the splice, a holds five elements and b is empty, because its nodes now belong to a.

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.

Start withWhy
push_back()Add an element at the end
insert()Add an element before an iterator
splice()Move nodes from one list to another
sort()Sort the list with the member function

Full list: C++ std::list Functions index (/cpp/list/function).

Frequently Asked Questions

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.

Browse every list function

Open the functions index for categorized guides with examples and FAQs.

List Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful