C++ map rbegin() Function
Example
Get the last element of a map:
map<string, int> people = {{"John", 32}, {"Adele", 45}, {"Bo", 29}};
auto it = people.rbegin();
cout << it->first << " is: " << it->second;
Definition and Usage
The rbegin() function returns a reverse iterator to the last element of a map. A reverse iterator is an iterator (a pointer-like object that points to an element) that moves backwards: ++ moves it to the previous element.
Map elements are sorted by key, so the last element is the one with the largest key.
The rbegin function is defined in the <map> header file.
Tip: Use rbegin() together with rend() to loop through a map in reverse order.
Syntax
map.rbegin()
Parameter Values
None.
Technical Details
| Returns: | A reverse iterator pointing to the last element of the map. |
|---|
Related Pages
Read more about maps in our Maps Tutorial.