C++ std::list rbegin() Function

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

Definition

In C++, std::list::rbegin() returns a reverse iterator that points at the last element of a std::list. When you use ++ on it, it moves toward the first element.

That makes it easy to loop through a list backwards without changing the list. Pair it with rend(). 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 rbegin with empty brackets and use it with rend().

C++
for (auto it = l.rbegin(); it != l.rend(); ++it) {
    // visits the last element first
}
int last = *l.rbegin();        // same as l.back()
CallWhat it does
l.rbegin()Returns a reverse_iterator to the last element. On an empty list it equals rend().

With reverse iterators, ++ goes backwards through the list. You do not write -- in the loop.

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

Parameters

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

Return Value

Return ValueDescription
reverse_iteratorA reverse iterator that points at the last element. On an empty list it equals rend().

C++ Examples

Example 1 — rbegin() and rend() in a Loop

Print a list from the last element to the first one, without changing it.

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

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

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

How It Works

  1. l.rbegin() points at the last node, which holds 3.
  2. Each ++it moves one step toward the front of the list, so the order is 3, then 2, then 1.
  3. The loop ends at l.rend(). The list itself is not reversed, only the way we walk through it.

Example 2 — rbegin() Is the Last Element

Dereferencing rbegin() gives the same element as back(), and you can write to it.

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

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

    *l.rbegin() = 99;

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

How It Works

  1. *l.rbegin() is a reference to the last element, so it can be assigned to.
  2. *l.rbegin() = 99 changes 30 to 99 inside the list.
  3. back() and *l.rbegin() reach the same node, so both lines print 99.

Example 3 — rbegin() and base() to Erase

Find the last 2 by searching from the back, and then erase it with a normal iterator.

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

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

    auto r = std::find(l.rbegin(), l.rend(), 2);
    l.erase(std::next(r).base());

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

How It Works

  1. std::find() with reverse iterators searches from the back, so r shows the 2 that is nearest to the back, the one at position four.
  2. r.base() would point one element after it, at 4. Taking std::next(r).base() gives an iterator to the element that r shows.
  3. erase() removes that node, so only the last 2 is gone and the output is 1 2 3 4.

Frequently Asked Questions

A reverse iterator that points at the last element of the list. Using ++ on it moves toward the first element.
Use for (auto it = l.rbegin(); it != l.rend(); ++it). Each ++ moves one element closer to the front.
No. The list is not changed. Only the direction of the walk is reversed. To really reverse the list, use reverse().
It returns the matching normal iterator, which points one element after the element that the reverse iterator shows. Use std::next(r).base() to get an iterator to the same element.
back() returns a reference to the last element. rbegin() returns a reverse iterator to it, which you can move through the list.

Next: remove()

Erase every element equal to a value with std::list.

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

14 people found this page helpful