Home C++ Sets Functions crend() C++ std::set crend() Function Definition In C++11, std::set::crend() returns a read-only reverse iterator to the position before the first value . It ends a reverse walk that began at crbegin().
It is a marker, not a value, so you must never dereference it. Algorithms such as std::find_if use it as the “nothing found” result. 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 crend with empty brackets. Use it as the stop value of a read-only reverse loop.
for (auto p = s.crbegin(); p != s.crend(); ++p) {
// read only, biggest first
}
auto n = std::distance(s.crbegin(), s.crend()); Call What it does s.crend()Returns a const_reverse_iterator one step before the first value, even when the set is not const.
crbegin() and crend() are always used together. On an empty set they are equal, so any loop or algorithm over them does nothing.
Library declarations const_reverse_iterator crend() const noexcept; // C++11 Parameters Parameter Description None crend() takes no parameters. Call it with empty brackets.
Return Value Return Value Description const_reverse_iteratorA read-only reverse iterator to the position before the first value. It equals crbegin() for an empty set.
Important
Never dereference crend() . When std::find_if(s.crbegin(), s.crend(), ...) returns crend(), it means no value matched. Check for that before you read the result.
C++ Examples 1. Read-only reverse loop 2. Measure the reverse range 3. Search from the back Example 1 — crend() Ends a Reverse Loop Print the values from the last to the first with iterators that cannot change anything.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> s = {"a", "b", "c"};
for (auto it = s.crbegin(); it != s.crend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
} How It Works crbegin() starts at c, which has the biggest value.Every ++it moves one value toward the front: c, then b, then a. After a, the next step reaches crend(), which is before the first value. The loop stops and nothing was changed. Example 2 — crend() with std::distance The distance from crbegin() to crend() counts every value once.
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::set<int> s = {4, 2, 3};
auto steps = std::distance(s.crbegin(), s.crend());
std::cout << "steps: " << steps << '\n';
std::cout << "size: " << s.size() << '\n';
return 0;
} How It Works std::distance counts the steps from the first iterator to the second.Going from the last value back to the position before the first value takes one step per value, so there are 3 steps. The number equals size(). Use size() in real code: it is instant, while distance has to walk through the whole set. Example 3 — crend() with std::find_if Find the biggest value that is below a limit, searching from the back.
#include <algorithm>
#include <iostream>
#include <set>
int main() {
std::set<int> temp = {12, 30, 15, 41};
auto it = std::find_if(temp.crbegin(), temp.crend(),
[](int v) { return v < 20; });
if (it == temp.crend()) {
std::cout << "none\n";
} else {
std::cout << "last under 20: " << *it << '\n';
}
return 0;
} How It Works std::find_if checks the values one by one from crbegin() toward crend(), so the largest values are tested first: 41, then 30, then 15.The lambda is true when the value is below 20. Value 15 matches, so the search stops there. If no value matched, the algorithm would return crend(). The program checks for that first, so it never reads from the marker. Frequently Asked Questions What does std::set::crend() return? A const_reverse_iterator to the position before the first value. It marks the end of a read-only reverse walk.
Can I dereference crend()? No. It does not point at a real value.
What is the difference between rend() and crend()? Both mark the end of a reverse walk. crend() always returns a const_reverse_iterator, so it is read-only.
What does an algorithm return when it reaches crend()? It means nothing matched. For example, std::find_if returns crend() when no value satisfies the test.
Which C++ version has crend()? C++11.
Next: emplace() Keep going through the std::set functions. Build a value directly inside the set (C++11).
emplace() 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.
14 people found this page helpful
Helpful Share Copy link Suggestion