Set Crend Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Crend Function

Set Crend Function

BLUF: Mastering Set Crend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Crend Function

C++ is renowned for its efficiency. Learn how Set Crend Function enables low-level control and high-performance computing in the tutorial below.

C++ set crend

The C++ set crend function is employed to provide a constant iterator pointing to the end of the set in reverse order, which is not the last element but the one past the last element. It is akin to the element that comes before the first element in a container that is not reversed.

Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

An unchanging iterator is a type of iterator that points to data that cannot be modified.

Syntax

Example

const_reverse_iterator crend() const noexcept;       //since C++ 11

Parameter

Return value

It provides a constreverseiterator pointing to the element subsequent to the final element in the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements of a set is considered to be a secure operation.

Exception Safety

This function never throws exception.

Example 1

Let's explore a basic example demonstrating the usage of the crend function:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset = {40,20,50,10,30};

  cout << "myset in reverse order:";
  for (auto rit=myset.crbegin(); rit != myset.crend(); ++rit)
    cout << ' ' << *rit;

  cout << '\n';

  return 0;
}

Output:

Output

myset in reverse order: 50 40 30 20 10

In the example provided, the crend method is employed to yield a constant reverse iterator pointing to the element that precedes the first element of the reversed container.

Due to the fact that sets organize elements based on key values in a sorted manner, iterating through a set will produce the same order, which is the sorted order of keys.

Example 2

Let's explore a basic illustration of iterating through a set in reverse order using a while loop:

Example

#include <iostream>
#include <set>
#include <string>
#include <iterator>

using namespace std;
 
int main() {
 
	// Creating & Initializing a set of String & Ints
	set<string> setEx = {"ccc", "ddd", "aaa", "bbb"};
 
	// Create a set iterator and point to the end of set
	set<string>::const_reverse_iterator it = setEx.crbegin();
 
	// Iterate over the set using Iterator till beginning.
	while (it != setEx.crend()) {
		// Accessing KEY from elemencpp tutorialed by it.
		string word = *it;
 
		cout << word << endl;
 
		// Increment the Iterator to point to next entry
		it++;
	}
	return 0;
}

Output:

Output

ddd
ccc
bbb
aaa

In the previous example, we are utilizing a while loop to iterate over the set in reverse order.

Since sets organize elements based on the sorted order of keys, iterating through a set will produce the same sequence, which is the sorted order of keys.

Example 3

Let's see a simple example:

Example

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
  set<int> c = {3, 1, 2};

  for_each(c.crbegin(), c.crend(), [](const int& x) {
    cout << x << endl;
  });
}

Output:

In the example provided, the elements of the set are returned in reverse sequence.

Example 4

Let's explore a basic illustration demonstrating the process of arranging and determining the top score:

Example

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
  set<int> emp = {1000,2500,4500,1200,3000};

   cout << "Salary"<< '\n';
   cout<<"______________________\n";
   
  set<int>::const_reverse_iterator rit;
  for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
    cout << *rit<< '\n';

    auto ite = emp.crbegin();
 
    cout << "\nHighest salary: "<< *ite <<" \n";

  return 0;
  }

Output:

Output

Salary
______________________
4500
3000
2500
1200
1000

Highest salary: 4500

In the scenario described, a set named emp is utilized to store salaries as keys. This allows us to benefit from the automatic sorting of salaries in the set, facilitating the identification of the highest salary.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience