Home C++ Deques Functions rend() C++ std::deque rend() Function Definition In C++, std::deque::rend() returns a reverse iterator that marks the end of a backwards walk. It stands for the place just before the first element, so it is not a real element.
It is the partner of rbegin(). Together they describe the deque from back to front. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue . A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax Call rend with empty brackets. It takes no arguments.
auto stop = d.rend();
auto first = d.rend() - 1; // only if d is not empty Call What it does d.rend()Returns a reverse iterator that marks the end of a reverse walk. d.rend() - 1Steps back onto the first element. This is safe only when the deque is not empty.
For a read-only version, use crend().
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 iterator A reverse_iterator to the place before the first element. It is only for comparing, never for reading. Empty deque For an empty deque, rend() equals rbegin().
C++ Examples 1. Reverse loop 2. First element 3. Search from back Example 1 — Stop a Reverse Loop with rend() The loop runs while the reverse iterator is not equal to rend().
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {2, 3, 4};
for (auto it = d.rbegin(); it != d.rend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
} How It Works rbegin() starts on 4, the last element.After 2, one more ++it makes the iterator equal to rend(). The condition it != d.rend() is now false, so the loop stops before reading anything invalid. Example 2 — Reach the First Element with rend() - 1 Step back from the end marker onto the first element.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {5, 6, 7};
auto it = d.rend() - 1;
std::cout << "*(rend() - 1): " << *it << '\n';
std::cout << "front(): " << d.front() << '\n';
return 0;
} *(rend() - 1): 5
front(): 5 How It Works d.rend() is the marker before the first element, so it cannot be read.- 1 moves the reverse iterator one step back in the reverse direction, which lands on the first element, 5.This is the same value as front(). In real code, front() is simpler. Example 3 — Search from the Back and Compare with rend() Find the last value above a limit. If nothing matches, the result equals rend().
#include <algorithm>
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {5, 20, 3, 15, 1};
auto a = std::find_if(d.rbegin(), d.rend(), [](int n) { return n > 10; });
auto b = std::find_if(d.rbegin(), d.rend(), [](int n) { return n > 100; });
if (a != d.rend()) {
std::cout << "last value above 10: " << *a << '\n';
}
if (b == d.rend()) {
std::cout << "none above 100\n";
}
return 0;
} last value above 10: 15
none above 100 How It Works Searching with reverse iterators starts from the back, so find_if sees 1, then 15. 15 is above 10, so a points to it and a != d.rend() is true. The match closest to the back wins, even though 20 also fits.No value is above 100, so find_if walks all the way and returns rend(). That is how you detect not found . Frequently Asked Questions What does std::deque::rend() return? It returns a reverse iterator that marks the end of a backwards walk. It stands for the place just before the first element.
Can I dereference rend()? No. It does not point to an element. Reading *d.rend() is undefined behavior.
What is the difference between rend() and begin()? begin() points to the first element and moves forward. rend() is a reverse marker before the first element and is used to stop a backwards walk.
Why do I need rend() and not end() in a reverse loop? Reverse iterators and normal iterators are different types. A reverse loop must compare against a reverse end marker, which is rend().
Is rend() equal to rbegin() on an empty deque? Yes. Both mark the same spot, so a loop between them does nothing.
Next: resize() Change the number of elements, adding or removing at the back with std::deque.
resize() 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.
15 people found this page helpful
Helpful Share Copy link Suggestion