C++ std::list crend() Function

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

Definition

In C++11 and later, std::list::crend() returns a const reverse iterator that stands one place before the first element of a std::list. It is the stop sign for a read-only backwards walk.

It is the partner of crbegin(). Like rend(), it does not refer to a real element. 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 crend with empty brackets and use it as the stop point of a read-only backwards walk.

C++
for (auto it = l.crbegin(); it != l.crend(); ++it) { }
auto r = std::find_if(l.crbegin(), l.crend(), pred);
bool none = (r == l.crend());
CallWhat it does
l.crend()Returns a const_reverse_iterator to the position before the first element.

crbegin() and crend() always come as a pair, like cbegin() and cend().

Library declarations
C++
const_reverse_iterator crend() const noexcept;   // since C++11

Parameters

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

Return Value

Return ValueDescription
const_reverse_iteratorA read-only reverse iterator to the position before the first element. It must not be dereferenced.

C++ Examples

Example 1 — crend() with std::find_if()

Search from the back for the last element that is bigger than 15.

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

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

    auto r = std::find_if(l.crbegin(), l.crend(), [](int x) { return x > 15; });

    if (r != l.crend()) {
        std::cout << "last big one: " << *r << '\n';
    }
    return 0;
}

How It Works

  1. The search starts at crbegin(), the node with 5. It is not bigger than 15, so the search goes on.
  2. The next element from the back is 30. The test passes, so find_if() stops and returns an iterator to it.
  3. If nothing had passed, the result would be crend(). Here r != l.crend() is true, so *r is safe to read.

Example 2 — crend() with std::distance()

The distance between crbegin() and crend() is the number of elements.

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

int main() {
    std::list<int> l = {7, 8, 9, 10};

    auto steps = std::distance(l.crbegin(), l.crend());

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

How It Works

  1. std::distance() counts how many ++ steps lead from the first iterator to the second.
  2. Going from crbegin() to crend() visits all four elements, so it takes four steps.
  3. The result equals size(). For counting, size() is faster, because the list stores the count.

Example 3 — crbegin() Equals crend() When Empty

A read-only backwards loop does nothing on an empty list.

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

int main() {
    std::list<int> l;
    int visited = 0;

    for (auto it = l.crbegin(); it != l.crend(); ++it) {
        ++visited;
    }

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

How It Works

  1. An empty list has no nodes, so crbegin() and crend() are the same iterator.
  2. The loop condition it != l.crend() is false from the very start, so the body never runs.
  3. The counter visited keeps its starting value 0. A backwards loop over an empty list is safe.

Frequently Asked Questions

A const reverse iterator that stands one place before the first element. It marks the end of a read-only backwards walk.
They mark the same place. rend() on a non-const list gives a reverse iterator that can change elements. crend() always gives a read-only one.
No. It does not refer to a real element, so dereferencing it is undefined behavior.
C++11 and later.
It returns crend() (or rend()). Compare the result with l.crend() before you dereference it.

Next: emplace()

Build a new element in place before an iterator (C++11) with std::list.

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

15 people found this page helpful