Javatpoint Logo
Javatpoint Logo

C++ multimap rend() function

The C++ multimap rend() function is used to return an iterator to the end of the multimap (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container.

Note:-This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

Syntax

Parameter

None

Return value

It returns a reverse iterator to the element following the last element of the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed. Neither the const nor the non-const versions modify the container.

Exception safety

This function never throws exception.

Example 1

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

Output:

c = 100
b = 400
b = 200
a = 100

In the above example, rend() function is used to return a reverse iterator to the element following the last element of the reversed container.

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 iterate over the multimap in reverse order.

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

Output:

The last element of the reversed multimap m1 is 1.
The multimap is: 1 2 3 .
The reversed multimap is: 3 2 1 .
After the erasure, the last element in the reversed multimap is 2.

In the above example, elements of multimap returned in reverse order.

Example 4

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

Output:

Salary | ID
______________________
4500   |  30
3500   |  50
3500   |  20
3000   |  40
1000   |  10
Highest salary: 4500 
ID is: 30

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

Next TopicC++ Multimap




Please Share

facebook twitter google plus pinterest