Home C++ Sets Functions rend() C++ std::set rend() Function 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.
for (auto p = s.rbegin(); p != s.rend(); ++p) {
// biggest value first
} Call What 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 reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept; Parameters Parameter Description None rend() takes no parameters. Call it with empty brackets.
Return Value Return Value Description reverse_iteratorA reverse iterator to the position before the first value. It equals rbegin() for an empty set.
C++ Examples 1. End of a reverse loop 2. An empty set 3. Top three values Example 1 — rend() Stops a Reverse Loop Print numbers from the largest to the smallest.
#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 The set holds the order 3, 4, 5. rbegin() starts at 5. Each ++it moves toward smaller values. After 3, the iterator equals rend(). 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.
#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;
} empty: true
with one value: false How It Works With no values, there is nothing to start on, so rbegin() and rend() are the same position. The first comparison is therefore true: a reverse loop would not run at all. 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.
#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 The largest scores sit at the back of the set. rbegin() starts at 90. The loop has two stop conditions: it != rend() protects against a set with fewer than three values, and shown < 3 limits the output. It prints 90, 85, and 70. The value 50 is never reached because the limit of three stops the loop first. Frequently Asked Questions What does std::set::rend() return? A reverse iterator to the position before the first value. It marks the end of a reverse walk.
Can I dereference rend()? No. It does not point at a value, so that is undefined behavior.
What is rend() on an empty set? It equals rbegin(), so a reverse loop does nothing.
Can I compare rend() with end()? No. They are different iterator types. Compare rend() with other reverse iterators, such as rbegin().
In what order does a loop from rbegin() to rend() visit the values? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion