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

Set Rbegin Function

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

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

C++ set rbegin

The C++ set rbegin method is employed to retrieve a reverse iterator pointing to the final element within the set data structure.

A reverse iterator for a set moves in the opposite direction and increments towards the start (first element) of the set container.

Syntax

Example

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

Parameter

Return value

It yields a reverse iterator that references the final element within the set.

Complexity

Constant.

Iterator validity

No changes.

Data Races

Accessing the set does not result in any modifications to the set container, whether in the const or non-const form. It is considered safe to access the elements of a set concurrently.

Exception Safety

This function never throws exception.

Example 1

Let's examine a basic illustration for the rbegin method:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset= {10,50,30,40,20};
  
  // show content:
  cout<<"Elements are: "<<endl;
  set<int>::reverse_iterator rit;
  for (rit=myset.rbegin(); rit!=myset.rend(); ++rit)
    cout << *rit<< '\n';

  return 0;
}

Output:

Output

Elements are: 
50
40
30
20
10

In the example provided, the rbegin method is utilized to retrieve a reverse iterator that points to the final element within the myset set.

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

Example 2

Let's consider a basic illustration demonstrating the process of iterating through a set in reverse sequence using a while loop:

Example

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

using namespace std;
 
int main() {
 
	// Creating & Initializing a set of String 
	set<string> setEx = {"aaa", "ccc", "ddd", "bbb"};
 
	// Create a set iterator and point to the end of set
	set<string, int>::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 instance, a while loop is employed to traverse the set in reverse, with the rbegin function setting the last element of the set.

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

Example 3

Let's explore a basic example to retrieve the initial element of the inverted collection:

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.rbegin( );  
   cout << "The first element in the reversed set is "  
        << *s1_rIter << "." << endl;  
  
   // begin can be used to start 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;  
  
   // rbegin can be used to start 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;  
  
   // A set element can be erased by dereferencing to its key   
   s1_rIter = s1.rbegin( );  
   s1.erase ( *s1_rIter );  
  
   s1_rIter = s1.rbegin( );  
   cout << "After the erasure, the first element "  
        << "in the reversed set is "<< *s1_rIter << "." << endl;  
        
return 0;        
}

Output:

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 explore a basic illustration to arrange and determine the top score:

Example

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

using namespace std;

int main ()
{
  set<int> marks = {400, 350, 465, 290, 410};

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

    auto ite = marks.rbegin();
 
    cout << "\nHighest Marks is: "<< *ite <<" \n";

  return 0;
  }

Output:

Output

Marks
______________________
465
410
400
350
290

Highest Marks is: 465

In the provided scenario, a collection of marks is utilized as the key elements. This approach allows us to leverage the automatic sorting capability within sets and determine the topmost mark.

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