Home » C++ multiset rbegin() function

C++ multiset rbegin() function

by Online Tutorials Library

C++ multiset rbegin()

C++ multiset rbegin() function is used to return a reverse iterator referring to the last element of the multiset container.

A reverse iterator of multiset moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multiset container.

Syntax

Parameter

None

Return value

It returns an iterator in reverse (reverse iterator) which points to the last element of the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The multiset is accessed neither the non-const nor the const versions modify the multiset container. Concurrently accessing the elements of a multiset is safe.

Exception Safety

This function never throws exception.

Example 1

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

Output:

Elements are:   40  30  20  20  10  10  

In the above example, rbegin() function is used to return a reverse iterator pointing to the last element in the mymultiset 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 2

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

Output:

ddd  ccc  bbb  bbb  aaa  aaa  

In the above example, we are using while loop to iterate over the multiset in reverse order and rbegin() 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 in the reversed multiset is 30.  The multiset is: 10 20 20 30  The reversed multiset is: 30 20 20 10  After the erasure, the first element in the reversed multiset is 20.  

Example 4

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

Output:

Marks  ______________________  465  450  450  410  410  290    Highest Marks is: 465   

In the above example, a multiset marks is implemented where the marks are the keys. This enables us to take advantage of the auto sorting in multisets and lets us to identify the highest marks. ,

Next TopicC++ multiset

You may also like