C++ multiset Library - rend() Function



Description

The multiset::rend() function in C++ STL returns a reverse iterator that points to an element that is present just before the first element in the multiset container. It is just a theoretical value and does not point to any element. It does not accept any parameters.

Syntax

Following are the syntax of multiset::rend in various C++ versions −

reverse_iterator rend(); //C++98
reverse_iterator rend() noexcept; //C++11 onwards

Return value

It returns a reverse iterator. This reverse iterator points to the element just before the first element.

Exceptions

It never throws exceptions.

Time complexity

The time complexity of multiset::rend() function is constant.

Examples of multiset::rend() Function

The following examples demonstrate the usage of multiset::rend() function in multiset −

Printing Elements of multiset in Reverse Order

In this example, we have used the rend() function with the rbegin() function to iterate the multiset elements in the reverse order −

#include <iostream>
#include <set>
using namespace std;
int main(){
   multiset<int> nums = {13, 31, 7, 53, 67, 53};
   cout << "Multiset elements in reverse order:\n";
   for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
      cout << *it << " ";
   }
   return 0;
}

The output of the above code is given below −

Multiset elements in reverse order:
67 53 53 31 13 7 

Getting Smallest Element using rend()

To get the smallest element in the multiset, the rend() function can be used. Since, by default multiset sorts the elements in ascending order, the rend() function returns the reverse iterator pointing just before first element. We have used --it to move pointer to first element i.e., smallest element of the multiset.

#include <iostream>
#include <set>
using namespace std;

int main(){
   multiset<int> nums = {10, 30, 20, 40, 30};
   cout << "Multiset elements:";
   for (auto it = nums.begin(); it != nums.end(); ++it)
   cout << " " << *it;

   auto it = nums.rend();
   // Move one step to right
   --it; 
   cout << "\nSmallest element using rend(): " << *it << endl;

   return 0;
}

The output of the above code is given below −

Multiset elements: 10 20 30 30 40
Smallest element using rend(): 10

Checking if multiset is Empty using rend()

In this example, we have an empty multiset. To check if this multiset is empty or not, we compare the iterators returned using the rbegin() and rend() functions respectively. If both the iterators point to the same point, then the multiset is empty.

#include <iostream>
#include <set>
using namespace std;
int main(){
   multiset<int> mSet1 = {10, 30, 20, 40, 30};
   multiset<int> mSet2;
   if (mSet1.rbegin() == mSet1.rend()){
      cout << "Multiset1 is empty" << endl;
   } else {
      cout << "Multiset1 is not empty" << endl;
   }
   if (mSet2.rbegin() == mSet2.rend()) {
      cout << "Multiset2 is empty" << endl;
   } else {
      cout << "Multiset2 is not empty" << endl;
   }
   return 0;
}

The output of the above code is given below −

Multiset1 is not empty
Multiset2 is empty
multiset.htm
Advertisements