Skip to content
Related Articles

Related Articles

map rbegin() function in C++ STL
  • Difficulty Level : Expert
  • Last Updated : 24 Oct, 2018

The std::map::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.

Returns: It returns a reverse iterator which points to the last element of the map.

Time Complexity: O(1)



Below examples illustrate the map::rbegin() method:

Example 1:




// C++ Program to illustrate
// map::rbegin() method
  
#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;
}
Output:
Map contains following elements in reverse order
e = 5
d = 4
c = 3
b = 2
a = 1

Example 2:




// C++ Program to illustrate
// map::rbegin() method
  
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
  
    map<char, char> mp = {
        { 'a', 'A' },
        { 'b', 'B' },
        { 'c', 'C' },
        { 'd', 'D' },
        { 'e', 'E' },
    };
  
    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;
}
Output:
Map contains following elements in reverse order
e = E
d = D
c = C
b = B
a = A
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :