C++ std::list rend() Function

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

Definition

In C++, std::list::rend() returns a reverse iterator that stands one place before the first element of a std::list. It is the stop sign for a backwards walk.

Like end(), it does not refer to a real element and must not be dereferenced. It is the partner of rbegin(). 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 rend with empty brackets and use it as the stop point of a backwards loop.

C++
for (auto it = l.rbegin(); it != l.rend(); ++it) { }
auto r = std::find(l.rbegin(), l.rend(), 5);
bool none = (r == l.rend());
CallWhat it does
l.rend()Returns a reverse_iterator to the position before the first element.

A backwards loop always ends when the iterator equals rend(). On an empty list, rbegin() == rend() at once.

Library declarations
C++
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

Parameters

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

Return Value

Return ValueDescription
reverse_iteratorA reverse iterator to the position before the first element. It must not be dereferenced.

C++ Examples

Example 1 — rend() as the Loop Stop

The same backwards walk as a for loop, but written with while, to show where rend() is checked.

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

int main() {
    std::list<std::string> l = {"one", "two", "three"};

    auto it = l.rbegin();
    while (it != l.rend()) {
        std::cout << *it << '\n';
        ++it;
    }
    return 0;
}

How It Works

  1. it starts at rbegin(), the node with "three".
  2. Before every pass the condition it != l.rend() checks that the iterator still shows a real element.
  3. After "one" the next ++it reaches rend(), the condition fails, and the loop ends.

Example 2 — rend() and a Reverse Search

Find the last occurrence of a value and measure how far it is from the back.

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

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

    auto r = std::find(l.rbegin(), l.rend(), 5);

    if (r == l.rend()) {
        std::cout << "not found\n";
    } else {
        std::cout << "last 5 is " << std::distance(l.rbegin(), r)
                  << " steps from the back\n";
    }
    return 0;
}

How It Works

  1. std::find() starts at the last element 3, then checks 2, and then reaches the second 5.
  2. The iterator r is not equal to l.rend(), so the search succeeded. A value that is missing would give rend().
  3. std::distance(l.rbegin(), r) counts the steps from the back, which is 2.

Example 3 — rbegin() Equals rend() When Empty

On an empty list a backwards loop has nothing to visit.

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

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

    std::cout << std::boolalpha;
    std::cout << "rbegin == rend: " << (l.rbegin() == l.rend()) << '\n';

    l.push_back(1);
    std::cout << "rbegin == rend: " << (l.rbegin() == l.rend()) << '\n';
    return 0;
}

How It Works

  1. A list with no nodes has no last element, so rbegin() already stands at rend().
  2. A backwards loop would stop before its first pass, which is the safe behavior we want.
  3. After push_back(1) the list has an element, so the two iterators are different and the loop would run once.

Frequently Asked Questions

A reverse iterator that stands one place before the first element. It marks the end of a backwards walk.
No. It does not refer to a real element, so dereferencing it is undefined behavior.
A range that visits the elements from the last one to the first one. The range is half open, just like [begin(), end()).
It returns rend(). Compare the result with l.rend() to know if the value was found.
When the list is empty. There is nothing to walk through, so a backwards loop does not run at all.

Next: resize()

Change the number of elements with std::list.

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

8 people found this page helpful