Javatpoint Logo
Javatpoint Logo

C++ multimap crbegin() function

The C++ multimap crbegin() function is used to return a constant reverse iterator referring to the last element in the multimap container.

A constant reverse iterator of multimap moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multimap 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 multimap.

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed.

Exception safety

This function never throws exceptions.

Example 1

Let's see the simple example for crbegin() function:

Output:

mymultimap in reverse order: [b:Python] [b:C++] [a:Android] [a:Java]

In the above example, crbegin() function is used to return a constant reverse iterator pointing to the last element in the mymultimap multimap.

Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap will result in above order i.e. sorted order of keys.

Example 2

Let's see a simple example to iterate over the multimap in reverse order using while loop:

Output:

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

In the above example, we are using while loop to const_iterate over the multimap in reverse order and crbegin() function initializing the last element of the multimap.

Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap 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 multimap:

Output:

The first element of the reversed multimap m1 is: {2, 30}

In the above example, crbegin() function returns the first element of the reversed multimap m1 i.e. {2,30}.

Example 4

Let's see a simple example to sort and calculate the highest marks:

Output:

Marks | Roll Number
_____________________
480   |  50
400   |  30
400   |  10
300   |  40
300   |  20
Highest Marks is: 480 
Roll Number of Topper is: 50

In the above example, a multimap "marks" is implemented where the Roll Number is being stored as value and marks as key. This enables us to take advantage of the auto sorting in multimaps and lets us to identify the Roll number of the element with the highest marks.

Next TopicC++ Multimap




Please Share

facebook twitter google plus pinterest