C++ map rend() Function
Example
Loop through a map in reverse order:
map<string, int> people = {{"John", 32}, {"Adele", 45}, {"Bo", 29}};
for (auto it = people.rbegin(); it != people.rend(); ++it) {
cout << it->first << " is: " << it->second << "\n";
}
Definition and Usage
The rend() function returns a reverse iterator (an iterator that moves backwards) to the position before the first element of a map.
It does not point to a real element, so you cannot use it to read a value. It is used together with rbegin() to know when a reverse loop should stop.
The rend function is defined in the <map> header file.
Syntax
map.rend()
Parameter Values
None.
Technical Details
| Returns: | A reverse iterator pointing to the position before the first element of the map. |
|---|
Related Pages
Read more about maps in our Maps Tutorial.