Javatpoint Logo
Javatpoint Logo

C++ map crend() Function

C++ map crend() function is used to return a constant iterator to the end of the map (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.

Note: A constant iterator is an iterator that points to constant content.

Syntax

Parameter

None

Return value

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

Example 1

Let's see a simple example for crend() function.

Output:

z = 300
y = 200
x = 100

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

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

Example 2

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

Output:

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

In the above example, we are using while loop to const_iterate over the map in reverse order.

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

Example 3

Let's see a simple example.

Output:

Map contains following elements in reverse order:
e = 5
d = 4
c = 3
b = 2
a = 1

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

Example 4

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

Output:

Salary | ID
______________________
5500   | 50
4500   | 30
3000   | 40
2500   | 20
1000   | 10

Highest salary: 5500 
ID is: 50

In the above example, a map 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 maps and lets us to identify the ID of the element with the highest salary.

Next TopicC++ Map



Please Share

facebook twitter google plus pinterest