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

Multiset Rend Function

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

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

C++ multiset rend

The C++ multiset rend function is employed to retrieve an iterator pointing to the end of the multiset in reverse order, which is not the last element but rather the one past the last element. This behavior is analogous to the element that comes before the first element of the container when it 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() nothrow;                //since C++ 11

const_reverse_iterator rend() const nothrow;    //since C++ 11

Parameter

Return value

It provides a reverse iterator pointing to the element after the final element of the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is retrieved. Both the constant and non-constant variants do not alter the container.

Simultaneously accessing the elements of a multiset container is secure.

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 () {
   multiset<int> mymultiset = {30,40,20,10,20};

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

   return 0;
}

Output:

Output

Elements are : 40 30 20 20 10

In the previous instance, the rend method is employed to provide a reverse iterator pointing to the element after the last element of the reversed container.

Since multisets arrange elements based on the keys in sorted order, iterating through a multiset will produce the same order, which is the sorted sequence of keys.

Example 2

Let's explore a basic illustration of iterating over the multiset in reverse sequence utilizing a while loop:

Example

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

using namespace std;
 
int main() {
 
	// Creating & Initializing a multiset of String & Ints
	multiset<string> multisetEx = {"aaa", "bbb", "ccc", "ddd", "ccc"};
 
	// Create a multiset iterator and point to the end of multiset
	multiset<string>::reverse_iterator it = multisetEx.rbegin();
 
	// Iterate over the multiset using Iterator till beginning.
	while (it != multisetEx.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
ccc
bbb
aaa

In the example provided, we utilize a while loop to traverse the multiset in reverse. Multisets maintain elements in a sorted order based on keys. Consequently, iterating through a multiset will yield the aforementioned sequence, which corresponds to the sorted order of keys.

Example 3

Let's see a simple example:

Example

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

   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 ); 
   s1.insert( 10 );
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   cout << "The last element in the reversed multiset is "  
        << *s1_rIter << "." << endl;  
  
   // end can be used to terminate an iteration   
   // throught a multiset in a forward order  
   cout << "The multiset 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 multiset in a reverse order  
   cout << "The reversed multiset 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 multiset is " << *s1_rIter << "." << endl;  
}

Output:

Output

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

In the example provided, the items of the multiset are returned in reverse sequence.

Example 4

Let's explore a basic illustration for arranging and determining the top score:

Example

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

using namespace std;

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

   cout << "Salary" << '\n';
   cout<<"______________________\n";
   
  multiset<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
4500
3000
2500
1000

Highest salary: 5000

In the scenario mentioned, a multiset named emp is utilized to store salaries as values. This allows us to benefit from the automatic sorting feature in multisets and helps us determine the highest salary present.

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