C++ std::set rend() Function

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

Definition

In C++, std::set::rend() returns a reverse iterator to the position before the first value. It is the stop marker of a reverse walk that started at rbegin().

Like end(), it is a marker and not a real value, so you must never dereference 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 rend with empty brackets. Use it as the stop value of a reverse loop.

C++
for (auto p = s.rbegin(); p != s.rend(); ++p) {
    // biggest value first
}
CallWhat it does
s.rend()Returns a reverse_iterator one step before the first value. On a const set it returns a const_reverse_iterator.

rbegin() and rend() are a pair, like begin() and end(). On an empty set they are equal, so a reverse loop does nothing.

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 value. It equals rbegin() for an empty set.

C++ Examples

Example 1 — rend() Stops a Reverse Loop

Print numbers from the largest to the smallest.

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

int main() {
    std::set<int> byLength = {3, 5, 4};

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

How It Works

  1. The set holds the order 3, 4, 5. rbegin() starts at 5.
  2. Each ++it moves toward smaller values. After 3, the iterator equals rend().
  3. At that point the loop condition it != rend() fails, and the program prints the three lines from largest to smallest.

Example 2 — rend() on an Empty Set

Show that rbegin() equals rend() when there is nothing to walk.

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

int main() {
    std::set<std::string> s;
    std::cout << std::boolalpha;

    std::cout << "empty: " << (s.rbegin() == s.rend()) << '\n';

    s.insert("a");
    std::cout << "with one value: " << (s.rbegin() == s.rend()) << '\n';
    return 0;
}

How It Works

  1. With no values, there is nothing to start on, so rbegin() and rend() are the same position.
  2. The first comparison is therefore true: a reverse loop would not run at all.
  3. After insert("a"), rbegin() points at that one value, while rend() is still the position before it, so they differ.

Example 3 — rend() as a Safety Limit

Print up to three biggest scores. rend() keeps the loop safe when the set has fewer values.

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

int main() {
    std::set<int> board = {50, 90, 70, 85};

    int shown = 0;
    for (auto it = board.rbegin(); it != board.rend() && shown < 3; ++it, ++shown) {
        std::cout << *it << '\n';
    }
    return 0;
}

How It Works

  1. The largest scores sit at the back of the set. rbegin() starts at 90.
  2. The loop has two stop conditions: it != rend() protects against a set with fewer than three values, and shown < 3 limits the output.
  3. It prints 90, 85, and 70. The value 50 is never reached because the limit of three stops the loop first.

Frequently Asked Questions

A reverse iterator to the position before the first value. It marks the end of a reverse walk.
No. It does not point at a value, so that is undefined behavior.
It equals rbegin(), so a reverse loop does nothing.
No. They are different iterator types. Compare rend() with other reverse iterators, such as rbegin().
From the biggest value to the smallest value (or the reverse of your own comparator).

Next: size()

Keep going through the std::set functions. Count how many unique values are in the set.

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