map rbegin() function in C++ STL

The rbegin() is a function in C++ STL. It returns a reverse iterator which points to the last element of the map. The reverse iterator iterates in reverse order and incrementing it means moving towards beginning of map.

SYNTAX:

r_i rbegin();
const_r_i rbegin() const;

Parameters:
It does not except any parameters.



Return Value: This method throws a reverse iterator to the reverse beginning of the sequence container.

Time Complexity:

O(1)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <map>
using namespace std;
  
int main()
{
  
    map<char, int> mp = {
        { 'a', 1 },
        { 'b', 2 },
        { 'c', 3 },
        { 'd', 4 },
        { 'e', 5 },
    };
  
    cout << "Map contains "
         << "following elements in"
         << " reverse order"
         << endl;
  
    for (auto i = mp.rbegin(); i != mp.rend(); ++i) {
  
        cout << i->first
             << " = "
             << i->second
             << endl;
    }
  
    return 0;
}

chevron_right


Output:

Map contains following elements in reverse order
e = 5
d = 4
c = 3
b = 2
a = 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.