Home C++ Vectors Functions rend() C++ std::vector rend() Function Definition In C++, std::vector::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 vector from back to front. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array . Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax Call rend with empty brackets. It takes no arguments.
auto stop = v.rend();
auto first = v.rend() - 1; // only if d is not empty Call What it does v.rend()Returns a reverse iterator that marks the end of a reverse walk. v.rend() - 1Steps back onto the first element. This is safe only when the vector 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 vector For an empty vector, 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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {2, 3, 4};
for (auto it = v.rbegin(); it != v.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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {5, 6, 7};
auto it = v.rend() - 1;
std::cout << "*(rend() - 1): " << *it << '\n';
std::cout << "front(): " << v.front() << '\n';
return 0;
} *(rend() - 1): 5
front(): 5 How It Works v.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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {5, 20, 3, 15, 1};
auto a = std::find_if(v.rbegin(), v.rend(), [](int n) { return n > 10; });
auto b = std::find_if(v.rbegin(), v.rend(), [](int n) { return n > 100; });
if (a != v.rend()) {
std::cout << "last value above 10: " << *a << '\n';
}
if (b == v.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::vector::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 vector? Yes. Both mark the same spot, so a loop between them does nothing.
Next: reserve() Prepare storage so later push_back calls avoid reallocations with std::vector.
reserve() 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