C++ std::list cend() Function

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

Definition

In C++11 and later, std::list::cend() returns a const iterator that points one place after the last element of a std::list. Like end(), it is only a stop sign and not a real element.

It is the partner of cbegin() for loops and algorithms that only read the 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 cend with empty brackets. Compare iterators with it, but do not dereference it.

C++
for (auto it = l.cbegin(); it != l.cend(); ++it) { }
int n = std::count_if(l.cbegin(), l.cend(), pred);
int last = *std::prev(l.cend());
CallWhat it does
l.cend()Returns a const_iterator to the position after the last element.

cbegin() and cend() always come as a pair. Mixing cbegin() with end() also compiles, but the pair is easier to read.

Library declarations
C++
const_iterator cend() const noexcept;   // since C++11

Parameters

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

Return Value

Return ValueDescription
const_iteratorA read-only iterator to the position after the last element. It must not be dereferenced.

C++ Examples

Example 1 — cend() with std::prev()

Step one place back from cend() to read the last element without being able to change it.

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

int main() {
    std::list<std::string> l = {"red", "green", "blue"};

    auto last = std::prev(l.cend());

    std::cout << "last: " << *last << '\n';
    return 0;
}

How It Works

  1. l.cend() is a read-only iterator after the last node, so it cannot be dereferenced.
  2. std::prev() moves it one step back, to the node that holds "blue". A list can do this quickly because every node knows its previous node.
  3. *last reads the text. Writing *last = "x" would not compile, because the iterator is const.

Example 2 — cend() with std::count_if()

Count how many elements pass a test, using only read-only iterators.

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

int main() {
    std::list<int> l = {1, 2, 3, 4, 5, 6};

    long evens = std::count_if(l.cbegin(), l.cend(),
                               [](int x) { return x % 2 == 0; });

    std::cout << "even numbers: " << evens << '\n';
    return 0;
}

How It Works

  1. std::count_if() looks at each element from cbegin() up to, but not including, cend().
  2. The lambda returns true for 2, 4, and 6.
  3. The count is 3. The list is only read, so the const iterators are a good fit.

Example 3 — cend() Compared with end()

The two iterators point to the same place. Only the type is different.

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

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

    std::list<int>::const_iterator c = l.cend();
    std::list<int>::iterator n = l.end();

    std::cout << std::boolalpha;
    std::cout << "same place: " << (c == n) << '\n';
    return 0;
}

How It Works

  1. c has the type const_iterator, and n has the type iterator. Both are taken from the same list.
  2. The two types can be compared with ==, because an iterator converts to a const_iterator.
  3. Both point just after the last node, so the answer is true. The only difference is that c cannot change elements.

Frequently Asked Questions

A const_iterator that points one place after the last element. It marks the end of a read-only range.
They point to the same place. end() on a non-const list returns an iterator that can change elements. cend() always returns a const_iterator.
No. It points after the last element, so dereferencing it is undefined behavior.
The pair tells the reader, and the compiler, that the loop or algorithm will not change the list.
C++11 and later.

Next: clear()

Erase every element from the list with std::list.

clear() 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.

12 people found this page helpful