TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set rbegin() Function

C++ set rbegin() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

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

      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

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:

#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:

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:

#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 element pointed by it.
		string word = *it;
  
		cout << word << endl;
 
		// Increment the Iterator to point to next entry
		it++;
	}
	return 0;
}

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:

#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:

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:

#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:

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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf