C++ multiset crbegin()
C++ multiset crbegin() function is used to return a constant reverse iterator referring to the last element in the multiset container.
A constant reverse iterator of multiset moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multiset container and points to the constant element.
Syntax
Parameter
None
Return value
It returns a constant reverse iterator pointing to the last element of the multiset.
Parameter
None
Return value
crbegin() function returns a constant reverse iterator pointing to the last element of the multimap.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Concurrently accessing the elements of a multiset container is safe.
Exception Safety
This function never throws exceptions.
Example 1
Let’s see the simple example for crbegin() function:
Output:
mymultiset in reverse order: 40 30 30 20 10 10
In the above example, crbegin() function is used to return a constant reverse iterator pointing to the last element in the mymultiset multiset.
Because multisets store the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.
Example 2
Let’s see a simple example to iterate over the multiset in reverse order using while loop:
Output:
ccc bbb bbb aaa
In the above example, we are using while loop to const_iterate over the multiset in reverse order and crbegin() function initializing the last element of the multiset.
Because multiset stores the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.
Example 3
Let’s see a simple example to get the first element of the reversed multiset:
Output:
The first element of the reversed multiset s1 is: 40
In the above example, crbegin() function returns the first element of the reversed multiset s1 i.e. 40.
Example 4
Let’s see a simple example to sort and calculate the highest marks:
Output:
Marks ______________________ 400 365 250 250 220 220 Highest Marks is: 400
In the above example, a multiset ‘marks’ is implemented where the elements of this multiset are stored as keys. Function crbegin() enables us to take advantage of the auto sorting in multisets and lets us to identify the highest marks.