C++ std::set crbegin() Function

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

Definition

In C++11, std::set::crbegin() returns a read-only reverse iterator (const_reverse_iterator) that starts at the last value, the biggest one.

The letters c and r stand for const and reverse. It is the right choice when you walk a set backwards and must not change it. 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 crbegin with empty brackets. Pair it with crend().

C++
auto it = s.crbegin();                  // biggest value, read-only
for (auto p = s.crbegin(); p != s.crend(); ++p) {
    // read only, biggest first
}
CallWhat it does
s.crbegin()Returns a const_reverse_iterator to the last value, even when the set is not const.

A function that takes const std::set& gets read-only iterators anyway. crbegin() is most useful when you hold a non-const set but want to promise that this loop only reads.

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

Parameters

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

Return Value

Return ValueDescription
const_reverse_iteratorA read-only reverse iterator to the last value. It equals crend() for an empty set.

C++ Examples

Example 1 — crbegin() Reads the Last Value

Print the biggest value using a read-only reverse iterator.

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

int main() {
    std::set<std::string> names = {"Ann", "Zoe", "Bob"};

    if (!names.empty()) {
        auto it = names.crbegin();
        std::cout << *it << '\n';
    }
    return 0;
}

How It Works

  1. The values sort as Ann, Bob, Zoe. The last one in that order is Zoe.
  2. crbegin() starts at that value, so *it is Zoe.
  3. The result is read-only, so the program cannot change the data by accident.

Example 2 — crbegin() in a Function

Pass a const set to a function that prints the values in reverse order.

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

void printReverse(const std::set<int>& s) {
    for (auto it = s.crbegin(); it != s.crend(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
}

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

How It Works

  1. The function takes a const reference, so it promises not to change the set. Calling crbegin() states the same promise in the loop.
  2. The iterator starts at 3 and each ++it moves toward 1.
  3. The loop stops at crend(), so the output is 3 2 1, in reverse sorted order.

Example 3 — crbegin() to Copy Values Backwards

Collect the values of a set into a vector, from the biggest to the smallest.

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

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

    std::vector<int> out;
    for (auto it = s.crbegin(); it != s.crend(); ++it) {
        out.push_back(*it);
    }

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

How It Works

  1. The set stores values in the order 2, 4, 7. The reverse walk visits them as 7, 4, 2.
  2. Each step pushes *it into the vector. The set itself is untouched.
  3. The vector now holds the values in descending order, which is what the second loop prints.

Frequently Asked Questions

A const_reverse_iterator that points to the last value of the set, which is the biggest value. It is read-only.
rbegin() on a non-const set returns reverse_iterator. crbegin() always returns a read-only reverse iterator.
C++11.
Not directly. Convert it to a normal iterator with std::next(it).base() and pass that to erase().
Yes. It equals crend(), but you must not dereference it.

Next: crend()

Keep going through the std::set functions. Get a read-only reverse iterator before the first value (C++11).

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

13 people found this page helpful