Home » C++ multiset rend() function

C++ multiset rend() function

by Online Tutorials Library

C++ multiset rend()

C++ multiset rend() function is used to return an iterator to the end of the multiset (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 constant nor the non-constant versions modify the container.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This function never throws exception.

Example 1

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

Output:

Elements are : 40 30 20 20 10  

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 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:

ddd  ccc  ccc  bbb  aaa  

In the above example, we are using while loop to iterate over the multiset in reverse order. 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 3

Let’s see a simple example:

Output:

The last element in the reversed multiset is 10.  The multiset is: 10 10 20 30 .  The reversed multiset is: 30 20 10 10 .  After erase, the last element in the reversed multiset is 20.  

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

Example 4

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

Output:

Salary  ______________________  5000  4500  4500  3000  2500  1000    Highest salary: 5000  

In the above example, a multiset emp is implemented where salary is stored as value. This enables us to take advantage of the auto sorting in multisets and lets us to identify the highest salary.

Next TopicC++ multiset

You may also like