multimap rend in C++ STL


In this article we will be discussing the working, syntax, and examples of multimap::rend() function 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::rend()?

multimap::rend() function is an inbuilt function in C++ STL, which is defined in  header file. rend() implies reverse end function, this function is the reverse of the end(). This function returns an iterator which is pointing to the element preceding the first element of the multimap container.

Syntax

multiMap_name.rend();

Parameter

This function accepts no parameter.

Return value

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

Input 

multimap<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.rend();

Output 

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 4, 40 });
   mul.insert({ 5, 50 });
   //displaying multimap elements
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto it = mul.rbegin(); it!= mul.rend(); ++it){
      cout << it->first << '\t' << it->second << '\n';
   }
   return 0;
}

Output

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

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

Updated on: 22-Apr-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements