Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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

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 multimap container and decreasing it results into moving to the end of the multimap container. Along with this, it cannot be used to modify the contents it points to, even if the multimap element is not itself constant.

Note: Multimap 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 below example, the multimap::crend function is used to return the const_reverse_iterator pointing to the element preceding the first element of the multimap MMap.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MMap;
  multimap<string, string>::const_reverse_iterator crit;

  MMap.insert(pair<string, string>("USA", "New York"));
  MMap.insert(pair<string, string>("USA", "Washington"));  
  MMap.insert(pair<string, string>("CAN", "Toronto"));
  MMap.insert(pair<string, string>("CAN", "Montreal"));
  MMap.insert(pair<string, string>("IND", "Delhi"));

  crit = MMap.crend();
  crit--;
  cout<<crit->first<<" => "<<crit->second<<"\n";
  crit--;
  cout<<crit->first<<" => "<<crit->second<<"\n";
  crit--;
  cout<<crit->first<<" => "<<crit->second<<"\n";
  return 0;
}

The output of the above code will be:

CAN => Toronto
CAN => Montreal
IND => Delhi

Example:

Lets see another example of multimap where multimap::crend function is used with multimap::crbegin function to specify a range including all elements of the multimap container.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MMap;
  multimap<string, string>::const_reverse_iterator crit;

  MMap.insert(pair<string, string>("USA", "New York"));
  MMap.insert(pair<string, string>("USA", "Washington"));  
  MMap.insert(pair<string, string>("CAN", "Toronto"));
  MMap.insert(pair<string, string>("CAN", "Montreal"));
  MMap.insert(pair<string, string>("IND", "Delhi"));

  cout<<"MMap contains:"<<"\n ";
  for(crit = MMap.crbegin(); crit != MMap.crend(); ++crit)
    cout<<crit->first<<" => "<<crit->second<<"\n ";

  return 0;
}

The output of the above code will be:

MMap contains:
 USA => Washington
 USA => New York
 IND => Delhi
 CAN => Montreal
 CAN => Toronto

❮ C++ - Multimap