Home » C++ set crend() Function

C++ set crend() Function

by Online Tutorials Library

C++ set crend()

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

A constant iterator is an iterator that points to the constant content.

Syntax

Parameter

None

Return value

It returns a const_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.

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 crend() function:

Output:

myset in reverse order: 50 40 30 20 10  

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 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 const_iterate over the set in reverse order.

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:

Output:

3  2  1  

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

Example 4

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

Output:

Salary  ______________________  4500  3000  2500  1200  1000    Highest salary: 4500   

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

You may also like