The crbegin function in C++ map is utilized to provide a constant reverse iterator pointing to the final element within the map container.
A reverse iterator for a map moves in the opposite direction, incrementing towards the first element of the map container while pointing to constant elements.
Syntax
const_reverse_iterator crbegin() const noexcept; //since C++ 11
Parameter
Return value
It provides a constant reverse iterator that points to the final element within the map.
Example 1
Let's examine a basic illustration for the crbegin method.
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
cout << "mymap in reverse order:";
for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
cout << " [" << rit->first << ':' << rit->second << ']';
cout << '\n';
return 0;
}
Output:
mymap in reverse order: [c:300] [b:100] [a:200]
In the previous example, the crbegin method is employed to provide a constant reverse iterator that points to the final element within the mymap map.
Since maps store elements in a sorted manner based on keys, iterating through a map will yield the same sequence, 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.
#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>::const_reverse_iterator it = mapEx.crbegin();
// Iterate over the map using Iterator till beginning.
while (it != mapEx.crend()) {
// 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
bbb :: 12
aaa :: 10
In the previously mentioned scenario, a while loop is employed to iterate constantly over the map in reverse order, with the crbegin function being utilized to initialize the final element of the map.
Due to the fact that maps store elements based on the sorted order of keys, iterating through a map will consequently yield 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 map.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<int,int> m1 = {
{ 1, 10},
{ 2, 20 },
{ 3, 30 } };
auto ite = m1.crbegin();
cout << "The first element of the reversed map m1 is: ";
cout << "{" << ite->first << ", "
<< ite->second << "}\n";
return 0;
}
Output:
The first element of the reversed map m1 is: {3, 30}
In the scenario mentioned, the crbegin method retrieves the initial element of the reversed map m1, specifically {3,30}.
Example 4
Let's examine a straightforward illustration demonstrating how to arrange and determine the top score.
#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>::const_reverse_iterator rit;
for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
cout << rit->first << " | " << rit->second << '\n';
auto ite = marks.crbegin();
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
400 | 10
312 | 20
300 | 40
Highest Marks is: 480
Roll Number of Topper is: 30
In the given scenario, a map is used to store marks as keys and Roll Numbers as values. This approach leverages the automatic sorting feature of maps, allowing us to easily determine the Roll number associated with the highest marks.