C++ std::list end() Function

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

Definition

In C++, std::list::end() returns an iterator that points one place after the last element of a std::list. It does not point at a real element.

It works as a stop sign. Loops run until the iterator reaches end(), and search functions return end() when they find nothing. 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 end with empty brackets. Compare iterators with it, but do not dereference it.

C++
auto stop = l.end();
bool done = (it == l.end());
l.insert(l.end(), 4);              // add at the end
int last = *std::prev(l.end());    // last element
CallWhat it does
l.end()Returns an iterator to the position after the last element. For a const list it returns a const_iterator.

Because a list is doubly linked, std::prev(l.end()) reaches the last element quickly. l.back() does the same and is shorter.

Library declarations
C++
iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

ParameterDescription
Noneend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
iteratorAn iterator to the position after the last element. It must not be dereferenced.

C++ Examples

Example 1 — end() Means “Not Found”

Search functions return end() when they find nothing. Compare the result with l.end().

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

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

    auto a = std::find(l.begin(), l.end(), 20);
    auto b = std::find(l.begin(), l.end(), 99);

    std::cout << (a != l.end() ? "found 20" : "no 20") << '\n';
    std::cout << (b != l.end() ? "found 99" : "no 99") << '\n';
    return 0;
}

How It Works

  1. std::find() walks from begin() up to, but not including, end(), looking for the value.
  2. For 20 it stops at a real node, so the iterator a is not equal to l.end().
  3. For 99 it walks all the way to the end and returns l.end(), so the test b != l.end() is false.

Example 2 — end() with std::prev()

Step one place back from end() to land on the last element.

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

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

    auto last = std::prev(l.end());
    *last = 99;

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. l.end() points after the last node, so we cannot read it. std::prev(l.end()) moves one step back.
  2. The result last points at the node with 30. *last = 99 writes into that node.
  3. The loop prints 10 20 99. This does the same as l.back() = 99.

Example 3 — end() as an Insert Position

Inserting before end() adds a new last element. The same end() iterator stays valid.

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

int main() {
    std::list<int> l = {1, 2, 3};
    auto stop = l.end();

    l.insert(l.end(), 4);

    std::cout << std::boolalpha;
    std::cout << "end unchanged: " << (stop == l.end()) << '\n';
    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. stop remembers the end() iterator before we change the list.
  2. insert(l.end(), 4) puts a new node before end(), so 4 becomes the last element.
  3. For a list, the end position does not move when you insert, so stop == l.end() is true. The output shows 1 2 3 4.

Frequently Asked Questions

An iterator that points one place after the last element. It is not a real element, so you must not dereference it.
It lets C++ use half-open ranges [begin, end). An empty list is then simply begin() == end(), and loops need only one test: it != end().
Use std::prev(l.end()) or --l.end(). It is shorter to call l.back(), which returns a reference to the last element.
The behavior is undefined. The program may crash or show garbage.
For std::list, an end() iterator that you stored stays equal to l.end() after insert(). Still, it is a good habit to call l.end() again in loops that change the list.

Next: erase()

Remove one element or a range of elements by iterator with std::list.

erase() tutorial →

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.

11 people found this page helpful