Javatpoint Logo
Javatpoint Logo

C++ set rbegin()

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

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

Syntax

Parameter

None

Return value

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

Complexity

Constant.

Iterator validity

No changes.

Data Races

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

Exception Safety

This function never throws exception.

Example 1

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

Output:

Elements are: 
50
40
30
20
10

In the above example, rbegin() function is used to return a reverse iterator pointing to the last element in the myset set.

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

Example 2

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

Output:

ddd
ccc
bbb
aaa

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

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

Output:

The first element in the reversed set is 30.
The set is: 10 20 30
The reversed set is: 30 20 10
After the erasure, the first element in the reversed set is 20.

Example 4

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

Output:

Marks
______________________
465
410
400
350
290

Highest Marks is: 465

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







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA