C++ Standard Library C++ STL Library

C++ multiset - crend() Function



The C++ multiset::crend function returns the constant reverse iterator (const_reverse_iterator) pointing to the element preceding the first element (reversed past-the-last element) of the multiset.

C++ crbegin crend

Note: A const_reverse_iterator is an iterator that points to constant value and iterates in backward direction. Increasing a const_reverse_iterator results into moving to the beginning of the multiset container and decreasing it results into moving to the end of the multiset container. Along with this, it cannot be used to modify the contents it points to, even if the multiset element is not itself constant.

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

Syntax

const_reverse_iterator crend() const noexcept;

Parameters

No parameter is required.

Return Value

A const_reverse_iterator to the reversed past-the-last element of the sequence container.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::crend function returns the const_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>::const_reverse_iterator crit;

  crit = MyMSet.crend();
  crit--;
  cout<<*crit<<" ";
  crit--;
  cout<<*crit<<" ";
  crit--;
  cout<<*crit<<" ";
  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::crend function is used with multiset::crbegin 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>::const_reverse_iterator crit;

  for(crit = MyMSet.crbegin(); crit != MyMSet.crend(); ++crit)
    cout<<*crit<<" ";

  return 0;
}

The output of the above code will be:

128 72 55 55 25 5  

❮ C++ <set> Library