C++ std::set rbegin() Function

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

Definition

In C++, std::set::rbegin() returns a reverse iterator that starts at the last value of the set. The last value is the biggest one.

With a reverse iterator, ++ moves toward smaller values. That lets you walk a set backwards, or get its biggest value quickly. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call rbegin with empty brackets. Stop at rend().

C++
auto it = s.rbegin();                  // biggest value
for (auto p = s.rbegin(); p != s.rend(); ++p) {
    // from biggest to smallest
}
CallWhat it does
s.rbegin()Returns a reverse_iterator to the last value. On a const set it returns a const_reverse_iterator.

A reverse iterator r has a method r.base() that gives the normal iterator one position after it. So std::next(r).base() is the normal iterator for the same value, which is useful for erase().

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 to the last value. It equals rend() for an empty set.

C++ Examples

Example 1 — rbegin() Points at the Biggest Value

Read the last value of a set without walking through all of them.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> s = {20, 5, 90};

    if (!s.empty()) {
        auto it = s.rbegin();
        std::cout << "biggest: " << *it << '\n';
    }
    return 0;
}

How It Works

  1. The set sorts its values to 5, 20, 90. The last value is 90.
  2. s.rbegin() starts a reverse walk at that last value, so *it is 90. It takes logarithmic time, with no loop.
  3. The !s.empty() check guards against an empty set, where rbegin() must not be dereferenced.

Example 2 — rbegin() in a Reverse Loop

Print the values from the biggest to the smallest.

C++
#include <iostream>
#include <set>

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

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

How It Works

  1. rbegin() starts on 3, the last value in the sorted set.
  2. For a reverse iterator ++it steps toward the front, so the values come out 3, 2, 1.
  3. The loop ends when it reaches rend(), the position before the first value. The set itself is not changed.

Example 3 — rbegin() and erase()

Remove the biggest value by turning the reverse iterator into a normal one.

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

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

    if (!s.empty()) {
        s.erase(std::next(s.rbegin()).base());   // remove 3
    }

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

How It Works

  1. erase() only accepts normal iterators, so s.erase(s.rbegin()) would not compile.
  2. base() gives the normal iterator one position after a reverse iterator, so std::next(s.rbegin()).base() is the normal iterator for the value at rbegin(), which is 3.
  3. That value is erased, and the loop prints what remains: 1 2.

Frequently Asked Questions

A reverse iterator that points to the last value of the set, which is the biggest value.
Use *s.rbegin(), after checking that the set is not empty. Or use *std::prev(s.end()).
Use s.erase(std::prev(s.end())), or convert the reverse iterator with std::next(s.rbegin()).base().
It moves toward smaller values, so the loop goes from the biggest value to the smallest.
rbegin() on a non-const set returns reverse_iterator. crbegin() always returns a read-only reverse iterator.

Next: rend()

Keep going through the std::set functions. Get the reverse iterator just before the first value.

rend() 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