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

Set Rend Function

BLUF: Mastering Set Rend 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 Rend Function

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

C++ set rend

The C++ set rend method is employed to provide an iterator pointing to the end of the set in reverse order, which is not the last element but the one past the last element. This behavior is analogous 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.

Syntax

Example

reverse_iterator rend();                             //until C++ 11
const_reverse_iterator rend() const;                 //until C++ 11
      reverse_iterator rend() noexcept;               //since C++ 11
const_reverse_iterator rend() const noexcept;   //since C++ 11

Parameter

Return value

It provides a reverse iterator pointing to the element following the final element in the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

Accessing the container does not result in any modifications, whether using the const or non-const versions.

It is secure to access the elements of a set simultaneously.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example for rend function:

Example

#include <iostream>
#include <set>

using namespace std;

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

   cout << "Elements are :";
   for (auto rit = myset.rbegin(); rit != myset.rend(); ++rit)
      cout << ' ' << *rit;
      cout << '\n';

   return 0;
}

Output:

Output

Elements are : 50 40 30 20 10

In the example provided, the rend method is utilized to generate a reverse iterator pointing to the element succeeding the final element of the reversed container.

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 2

Let's explore a basic illustration of iterating through a set in reverse order utilizing 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 = {"aaa", "bbb", "ccc", "ddd"};
 
	// Create a set iterator and point to the end of set
	set<string>::reverse_iterator it = setEx.rbegin();
 
	// Iterate over the set using Iterator till beginning.
	while (it != setEx.rend()) {
		// 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 employing a while loop to traverse the set in reverse sequence.

Because a set organizes its elements based on the sorted order of keys, iterating through a set will yield the same order - specifically, the sorted order of keys.

Example 3

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main() {  
   using namespace std;     
   set <int> s1;  
   set <int>::iterator s1_Iter;  
   set <int>::reverse_iterator s1_rIter;  

   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   cout << "The last element in the reversed set is "  
        << *s1_rIter << "." << endl;  
  
   // end can be used to terminate an iteration   
   // throught a set in a forward order  
   cout << "The set is: ";  
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )  
      cout << *s1_Iter << " ";  
   cout << "." << endl;  
  
   // rend can be used to terminate an iteration   
   // throught a set in a reverse order  
   cout << "The reversed set is: ";  
   for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )  
      cout << *s1_rIter << " ";  
   cout << "." << endl;  
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   s1.erase ( *s1_rIter );  
  
   s1_rIter = s1.rend( );  
   --s1_rIter;  
   cout << "After erase, the last element in the "  
        << "reversed set is " << *s1_rIter << "." << endl;  
}

Output:

Output

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

In the example mentioned above, the items of the set are given back in the opposite sequence.

Example 4

Let's explore a basic example to arrange and determine the highest score:

Example

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

using namespace std;

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

   cout << "Salary" << '\n';
   cout<<"______________________\n";
   
  set<int>::reverse_iterator rit;
    for (rit=emp.rbegin(); rit!=emp.rend(); ++rit)
    cout << *rit<< '\n';

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

  return 0;
  }

Output:

Output

Salary
______________________
5000
4500
3000
2500
1000

Highest salary: 5000

In the scenario described, a set named emp is utilized to store the salary as the value and the ID as the key. This approach leverages the automatic sorting capability of sets, allowing us to easily determine the ID associated with the highest salary value.

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