Home C++ Lists Functions rend() C++ std::list rend() Function Definition In C++, std::list::rend() returns a reverse iterator that stands one place before the first element of a std::list. It is the stop sign for a backwards walk.
Like end(), it does not refer to a real element and must not be dereferenced. It is the partner of rbegin(). Include <list> to use it.
This is a member of std::list<T>, a doubly linked list . Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax Call rend with empty brackets and use it as the stop point of a backwards loop.
for (auto it = l.rbegin(); it != l.rend(); ++it) { }
auto r = std::find(l.rbegin(), l.rend(), 5);
bool none = (r == l.rend()); Call What it does l.rend()Returns a reverse_iterator to the position before the first element.
A backwards loop always ends when the iterator equals rend(). On an empty list, rbegin() == rend() at once.
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 element. It must not be dereferenced.
C++ Examples 1. Loop with while 2. Search from the back 3. An empty list Example 1 — rend() as the Loop Stop The same backwards walk as a for loop, but written with while, to show where rend() is checked.
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> l = {"one", "two", "three"};
auto it = l.rbegin();
while (it != l.rend()) {
std::cout << *it << '\n';
++it;
}
return 0;
} How It Works it starts at rbegin(), the node with "three".Before every pass the condition it != l.rend() checks that the iterator still shows a real element. After "one" the next ++it reaches rend(), the condition fails, and the loop ends. Example 2 — rend() and a Reverse Search Find the last occurrence of a value and measure how far it is from the back.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {5, 1, 5, 2, 3};
auto r = std::find(l.rbegin(), l.rend(), 5);
if (r == l.rend()) {
std::cout << "not found\n";
} else {
std::cout << "last 5 is " << std::distance(l.rbegin(), r)
<< " steps from the back\n";
}
return 0;
} last 5 is 2 steps from the back How It Works std::find() starts at the last element 3, then checks 2, and then reaches the second 5.The iterator r is not equal to l.rend(), so the search succeeded. A value that is missing would give rend(). std::distance(l.rbegin(), r) counts the steps from the back, which is 2.Example 3 — rbegin() Equals rend() When Empty On an empty list a backwards loop has nothing to visit.
#include <iostream>
#include <list>
int main() {
std::list<int> l;
std::cout << std::boolalpha;
std::cout << "rbegin == rend: " << (l.rbegin() == l.rend()) << '\n';
l.push_back(1);
std::cout << "rbegin == rend: " << (l.rbegin() == l.rend()) << '\n';
return 0;
} rbegin == rend: true
rbegin == rend: false How It Works A list with no nodes has no last element, so rbegin() already stands at rend(). A backwards loop would stop before its first pass, which is the safe behavior we want. After push_back(1) the list has an element, so the two iterators are different and the loop would run once. Frequently Asked Questions What does std::list::rend() return? A reverse iterator that stands one place before the first element. It marks the end of a backwards walk.
Can I dereference rend()? No. It does not refer to a real element, so dereferencing it is undefined behavior.
What do rbegin() and rend() form together? A range that visits the elements from the last one to the first one. The range is half open, just like [begin(), end()).
What does a reverse search return when nothing is found? It returns rend(). Compare the result with l.rend() to know if the value was found.
When is rbegin() equal to rend()? When the list is empty. There is nothing to walk through, so a backwards loop does not run at all.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion