Map Rbegin Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Map Rbegin Function

Map Rbegin Function

BLUF: Mastering Map Rbegin Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Map Rbegin Function

C++ is renowned for its efficiency. Learn how Map Rbegin Function enables low-level control and high-performance computing in the tutorial below.

The C++ map rbegin method is utilized to retrieve a reverse iterator that points to the final element of the map data structure.

A reverse iterator of a map moves in the opposite direction, incrementing towards the start (first element) of the map container until it reaches there.

Syntax

Example

reverse_iterator rbegin(); //until C++ 11
const_reverse_iterator rbegin() const; //until C++ 11
      reverse_iterator rbegin() noexcept; //since C++ 11
const_reverse_iterator rbegin() const noexcept;  //since C++ 11

Parameter

Return value

It provides a reverse iterator that references the final element of the map.

Example 1

Let's see a simple example for rbegin function.

Example

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,int> mymap;
  
  mymap['x'] = 100;
  mymap['y'] = 200;
  mymap['z'] = 300;

  // show content:
  map<char,int>::reverse_iterator rit;
  for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';

  return 0;
}

Output:

Output

z = 300
y = 200
x = 100

In the example provided, the rbegin method is employed to retrieve a reverse iterator that points to the final element within the mymap container.

Since maps store elements in the sorted order of keys, iterating over a map will yield the same order, which is the sorted order of keys.

Example 2

Let's examine a basic illustration of iterating through a map in reverse order utilizing a while loop.

Example

#include <iostream>
#include <map>
#include <string>
#include <iterator>

using namespace std;
 
int main() {
 
	// Creating & Initializing a map of String & Ints
	map<string, int> mapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "bbb", 12 },
			{ "ccc", 13 }
	};
 
	// Create a map iterator and point to the end of map
	map<string, int>::reverse_iterator it = mapEx.rbegin();
 
	// Iterate over the map using Iterator till beginning.
	while (it != mapEx.rend()) {
		// Accessing KEY from elemencpp tutorialed by it.
		string word = it->first;
 
		// Accessing VALUE from elemencpp tutorialed by it.
		int count = it->second;
 
		cout << word << " :: " << count << endl;
 
		// Increment the Iterator to point to next entry
		it++;
	}
	return 0;
}

Output:

Output

ddd :: 11
ccc :: 13
bbb :: 12
aaa :: 10

In the example provided, a while loop is employed to traverse the map in reverse sequence, with the rbegin function initializing the final element of the map.

Since maps organize elements based on the keys in sorted order, iterating through a map will produce the same sequence, which is the sorted order of keys.

Example 3

Let's examine a basic illustration to retrieve the initial element of the reversed mapping.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,int> m1 = {
                { 1, 10},
                { 2, 20 },
                { 3, 30 } };
          
    auto ite = m1.rbegin();
 
    cout << "The first element of the reversed map m1 is: ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

Output:

Output

The first element of the reversed map m1 is: {3, 30}

In the aforementioned scenario, the rbegin method retrieves the initial element of the reversed mapping m1, which is {3,30}.

Example 4

Let's examine a basic illustration demonstrating the process of sorting and determining the highest score.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,int> marks = {
                { 400, 10},
                { 312, 20 },
                { 480, 30 },
                { 300, 40 },
                { 425, 50 }};

   cout << "Marks" << " | " << "Roll Number" << '\n';
   cout<<"______________________\n";
   
  map<int,int>::reverse_iterator rit;
  for (rit=marks.rbegin(); rit!=marks.rend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';

    auto ite = marks.rbegin();
 
    cout << "\nHighest Marks is: "<< ite->first <<" \n";
    cout << "Roll Number of Topper is: "<< ite->second << "\n";

  return 0;
  }

Output:

Output

Marks | Roll Number
______________________
480   | 30
425   | 50
400   | 10
312   | 20
300   | 40

Highest Marks is: 480 
Roll Number of Topper is: 30

In the scenario described, a mapping technique is utilized where the Roll Number is stored as the value and marks are stored as the key. This approach leverages the automatic sorting functionality in maps, allowing us to easily determine the Roll number associated with the highest marks.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience