C++ std::set crend() Function

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

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.

C++
for (auto p = s.crbegin(); p != s.crend(); ++p) {
    // read only, biggest first
}
auto n = std::distance(s.crbegin(), s.crend());
CallWhat 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
C++
const_reverse_iterator crend() const noexcept;   // C++11

Parameters

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

Return Value

Return ValueDescription
const_reverse_iteratorA read-only reverse iterator to the position before the first value. It equals crbegin() for an empty set.

C++ Examples

Example 1 — crend() Ends a Reverse Loop

Print the values from the last to the first with iterators that cannot change anything.

C++
#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

  1. crbegin() starts at c, which has the biggest value.
  2. Every ++it moves one value toward the front: c, then b, then a.
  3. 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.

C++
#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

  1. std::distance counts the steps from the first iterator to the second.
  2. Going from the last value back to the position before the first value takes one step per value, so there are 3 steps.
  3. 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.

C++
#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

  1. 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.
  2. The lambda is true when the value is below 20. Value 15 matches, so the search stops there.
  3. 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

A const_reverse_iterator to the position before the first value. It marks the end of a read-only reverse walk.
No. It does not point at a real value.
Both mark the end of a reverse walk. crend() always returns a const_reverse_iterator, so it is read-only.
It means nothing matched. For example, std::find_if returns crend() when no value satisfies the test.
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

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.

14 people found this page helpful