multimap::crbegin() and multimap::crend() in C++ STL


In this article we will be discussing the working, syntax, and examples of multimap::crbegin() and multimap::crend() functions in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::cbegin()?

multimap::crbegin() function is an inbuilt function in C++ STL, which is defined in <map> header file. crbegin() implies constant reverse begins, means the reverse of the cbegin which was constant begin, in other words, the function crbegin() will return the iterator which is pointing to the last element of the multimap container associated with the function. This iterator can’t be used to modify the multimap. This can be just used to traverse the set container.

Syntax

mutliMap_name.crbegin();

Parameter

This function doesn’t accept any parameter.

Return value

This function returns the iterator which is pointing to the last element of the container.

Input 

multimap<char, int> newmap;
newmap(make_pair(‘a’, 1));
newmap(make_pair(‘b’, 2));
newmap(make_pair(‘c’, 3));
newmap.crbegin();

Output 

c:3

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 2, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 1, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   auto it = mul.crbegin();
   cout<<"Last element using crbegin() is: {"<<it->first<< ", " << it->second << "}\n";
   cout <<"\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = mul.crbegin(); i!= mul.crend(); i++){
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Last element using crbegin() is: {5, 60}
Elements in multimap is :
KEY ELEMENT
5 60
2 20
2 30
1 10
1 40
1 50

What is multimap::crend()?

multimap::crend() function is an inbuilt function in C++ STL, which is defined in <map> header file. crend() implies constant reverse end iterator, means it is reverse of the cend which was a constant end iterator, in other words the function crend() will return the iterator which is pointing to the position just before the first position of the container associated with the function. This iterator can’t be used to modify the multimap. This can be just used to traverse the multimap container.

Syntax

newmultimap.crend();

Parameters

This function accepts no parameter.

Return value

It returns an iterator pointing to the preceding first element of the associated container.

Input 

multimap<char, int&lgt; newmap;
newmap(make_pair(‘a’, 1));
newmap(make_pair(‘b’, 2));
newmap(make_pair(‘c’, 3));
newmap.crend();

Output 

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 2, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 1, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = mul.crbegin(); i!= mul.crend(); i++){
      cout <<<; i->first << "\t" << i->second < endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements in multimap is :
KEY ELEMENT
5 60
2 20
2 30
1 10
1 40
1 50

Updated on: 22-Apr-2020

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements