The C++ multimap rbegin method is employed to retrieve a reverse iterator pointing to the final element within the multimap container.
A reverse iterator for a multimap moves in the opposite direction, incrementing towards the first element of the container until it reaches the start.
Syntax
reverse_iterator rbegin(); //until C++ 11
const_reverse_iterator rbegin() const; //until C++ 11
reverse_iterator rbegin() nothrow; //since C++ 11
const_reverse_iterator rbegin() const nothrow; //since C++ 11
Parameter
Return value
It provides a reverse iterator that points to the final element of the multimap.
Complexity
Constant.
Iterator validity
No changes.
Data races
The container is accessed without altering the container in either the const or non-const versions.
Exception safety
This function never throws exception.
Example 1
Let's examine a straightforward illustration for the rbegin method:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
mymultimap = {
{'a', 100},
{'b', 200},
{'a', 300},
{'c', 300}
};
// show content:
multimap<char,int>::reverse_iterator rit;
for (rit=mymultimap.rbegin(); rit!=mymultimap.rend(); ++rit)
cout << rit->first << " = " << rit->second << '\n';
return 0;
}
Output:
c = 300
b = 200
a = 300
a = 100
In the previously mentioned example, the rbegin method is employed to yield a reverse iterator that points to the final element within the mymultimap multimap.
Since a multimap organizes its elements based on the keys in sorted order, iterating through a multimap will yield the elements in the same order, which is the sorted order of keys.
Example 2
Let's explore a basic illustration of iterating through the multimap in reverse order utilizing a while loop:
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a multimap of String & Ints
multimap<string, int> multimapEx = {
{ "aaa", 10 },
{ "ddd", 11 },
{ "ccc", 12 },
{ "ccc", 13 }
};
// Create a multimap iterator and point to the end of multimap
multimap<string, int>::reverse_iterator it = multimapEx.rbegin();
// Iterate over the multimap using Iterator till beginning.
while (it != multimapEx.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:
ddd :: 11
ccc :: 13
ccc :: 12
aaa :: 10
In the previous instance, we are utilizing a while loop to traverse through the multimap in reverse sequence, with the rbegin function setting the initial value to the last element of the multimap.
Due to the fact that a multimap organizes its elements based on keys in a sorted manner, iterating through a multimap will yield the elements in the sorted order of keys.
Example 3
Let's examine a basic illustration to retrieve the initial element of the reversed multimap:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
multimap<int,int> m1 = {
{ 1, 10},
{ 2, 20 },
{ 3, 30 },
{ 3, 40 },
{ 4, 50}
};
auto ite = m1.rbegin();
cout << "The first element of the reversed multimap m1 is: ";
cout << "{" << ite->first << ", "
<< ite->second << "}\n";
return 0;
}
Output:
The first element of the reversed multimap m1 is: {4, 50}
The rbegin method retrieves the initial element of the reversed multimap m1, which in this case is {4,50}.
Example 4
Let's examine a basic illustration to arrange and compute the top scores:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
multimap<int,int> marks = {
{ 425, 10},
{ 300, 20 },
{ 480, 30 },
{ 300, 40 },
{ 425, 50 }};
cout << "Marks" << " | " << "Roll Number" << '\n';
cout<<"______________________\n";
multimap<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:
Marks | Roll Number
______________________
480 | 30
425 | 50
425 | 10
300 | 40
300 | 20
Highest Marks is: 480
Roll Number of Topper is: 30
In the scenario described, a multimap structure is utilized to store Roll Numbers as values and marks as keys. This approach leverages the automatic sorting functionality of multimaps, allowing us to easily determine the Roll number associated with the highest marks.