Home C++ Maps Functions rend() C++ std::map rend() Function Definition In C++, std::map::rend() returns a reverse iterator to the position before the first pair . It is the stop marker of a reverse walk that started at rbegin().
Like end(), it is a marker and not a real pair, so you must never dereference it. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs . Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call rend with empty brackets. Use it as the stop value of a reverse loop.
for (auto p = m.rbegin(); p != m.rend(); ++p) {
// biggest key first
} Call What it does m.rend()Returns a reverse_iterator one step before the first pair. On a const map it returns a const_reverse_iterator.
rbegin() and rend() are a pair, like begin() and end(). On an empty map they are equal, so a reverse loop does nothing.
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 pair. It equals rbegin() for an empty map.
C++ Examples 1. End of a reverse loop 2. An empty map 3. Top three keys Example 1 — rend() Stops a Reverse Loop Print the word lengths from the longest key to the shortest.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> byLength = {{3, "cat"}, {5, "horse"}, {4, "bird"}};
for (auto it = byLength.rbegin(); it != byLength.rend(); ++it) {
std::cout << it->first << " letters: " << it->second << '\n';
}
return 0;
} 5 letters: horse
4 letters: bird
3 letters: cat How It Works The key is the word length, so the map holds the order 3, 4, 5. rbegin() starts at 5. Each ++it moves toward smaller keys. After the pair for key 3, the iterator equals rend(). At that point the loop condition it != rend() fails, and the program prints the three lines from longest to shortest. Example 2 — rend() on an Empty Map Show that rbegin() equals rend() when there is nothing to walk.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m;
std::cout << std::boolalpha;
std::cout << "empty: " << (m.rbegin() == m.rend()) << '\n';
m["a"] = 1;
std::cout << "with one pair: " << (m.rbegin() == m.rend()) << '\n';
return 0;
} empty: true
with one pair: false How It Works With no pairs, there is nothing to start on, so rbegin() and rend() are the same position. The first comparison is therefore true: a reverse loop would not run at all. After m["a"] = 1, rbegin() points at that one pair, while rend() is still the position before it, so they differ. Example 3 — rend() as a Safety Limit Print up to three biggest scores. rend() keeps the loop safe when the map has fewer pairs.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> board = {{50, "Ann"}, {90, "Ben"}, {70, "Cy"}, {85, "Di"}};
int shown = 0;
for (auto it = board.rbegin(); it != board.rend() && shown < 3; ++it, ++shown) {
std::cout << it->first << " " << it->second << '\n';
}
return 0;
} How It Works The key is the score, so the largest scores sit at the back of the map. rbegin() starts at 90. The loop has two stop conditions: it != rend() protects against a map with fewer than three pairs, and shown < 3 limits the output. It prints 90, 85, and 70. The pair for 50 is never reached because the limit of three stops the loop first. Frequently Asked Questions What does std::map::rend() return? A reverse iterator to the position before the first pair. It marks the end of a reverse walk.
Can I dereference rend()? No. It does not point at a pair, so that is undefined behavior.
What is rend() on an empty map? It equals rbegin(), so a reverse loop does nothing.
Can I compare rend() with end()? No. They are different iterator types. Compare rend() with other reverse iterators, such as rbegin().
In what order does a loop from rbegin() to rend() visit the pairs? From the biggest key to the smallest key (or the reverse of your own comparator).
Next: size() Keep going through the std::map functions. Count the key and value pairs in the map.
size() 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