C++ Standard Library C++ STL Library

C++ multiset - rend() Function



The C++ multiset::rend function returns the reverse iterator pointing to the element preceding the first element (reversed past-the-last element) of the multiset. A reverse iterator iterates in backward direction and increasing it results into moving to the beginning of the multiset container. Similarly, decreasing a reverse iterator results into moving to the end of the multiset container.

C++ rbegin rend

Note: Multiset is an ordered data container which implies all its elements are ordered all the time.

Syntax

reverse_iterator rend();
const_reverse_iterator rend() const;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

Parameters

No parameter is required.

Return Value

A reverse iterator to the reversed past-the-last element of the sequence container. If the sequence object is constant qualified, the function returns a const_reverse_iterator, else returns an reverse_iterator.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::rend function returns the reverse iterator pointing to the element preceding the first element of the multiset MyMSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<string> MyMSet{"Alpha","Coding","Skills"};
  multiset<string>::reverse_iterator rit;

  rit = MyMSet.rend();
  rit--;
  cout<<*rit<<" ";
  rit--;
  cout<<*rit<<" ";
  rit--;
  cout<<*rit<<" ";
  return 0;
}

The output of the above code will be:

Alpha Coding Skills

Example:

Lets see another example where the multiset called MyMSet contains integer values and multiset::rend function is used with multiset::rbegin function to specify a range including all elements of the multiset container. Please note that, Multiset is an ordered data container.

#include <iostream>
#include <set>
using namespace std;
 
int main () {
  multiset<int> MyMSet{55, 25, 128, 5, 72, 55};
  multiset<int>::reverse_iterator rit;

  for(rit = MyMSet.rbegin(); rit != MyMSet.rend(); ++rit)
    cout<<*rit<<" ";

  return 0;
}

The output of the above code will be:

128 72 55 55 25 5 

❮ C++ <set> Library

5