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


The C++ multimap::crbegin function is used to return the constant reverse iterator (const_reverse_iterator) pointing to 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 crbegin() const noexcept;

Parameters

No parameter is required.

Return Value

A const_reverse_iterator to the reverse beginning of the sequence container.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the multimap::crbegin function is used to return the const_reverse_iterator pointing to the last element of the multimap MyMMap.

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

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

  crit = MyMMap.crbegin();
  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:

USA => Washington
USA => New York
IND => Delhi

Example:

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

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

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

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

  return 0;
}

The output of the above code will be:

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

❮ C++ <map> Library