Multimap Rend Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multimap Rend Function

Multimap Rend Function

BLUF: Mastering Multimap Rend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multimap Rend Function

C++ is renowned for its efficiency. Learn how Multimap Rend Function enables low-level control and high-performance computing in the tutorial below.

The C++ multimap rend method is employed to retrieve an iterator pointing to the conclusion of the multimap in reverse order, which is not the final element but the one beyond the final element. This mirrors the element that comes before the initial element in the container when it is not reversed.

Note:-This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

Syntax

Example

reverse_iterator rend();                              //until C++ 11
const_reverse_iterator rend() const;                 //until C++ 11
      reverse_iterator rend() nothrow;               //since C++ 11
const_reverse_iterator rend() const nothrow;  //since C++ 11

Parameter

Return value

It provides a reverse iterator pointing to the element that comes after the final element in the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed. Both the constant and non-constant variants do not alter the container.

Exception safety

This function never throws exception.

Example 1

Let's see the simple example for rend function:

Example

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,int> mymultimap;
  mymultimap =  {
                {'a', 100},
                {'b', 200},
                {'c', 100},
                {'b', 400}
                };
  // show content:
  multimap<char,int>::reverse_iterator rit;
  for (rit=mymultimap.rbegin(); rit!=mymultimap.rend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';
  return 0;
}

Output:

Output

c = 100
b = 400
b = 200
a = 100

In the previous instance, the rend method is employed to provide a reverse iterator pointing to the element that precedes the first element in the reversed container.

Since a multimap organizes its elements based on the keys in a sorted manner, looping through a multimap will yield the same sequence, which is the sorted order of keys.

Example 2

Let's explore a basic illustration of iterating through the multimap in reverse sequence using a while loop:

Example

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
	// Creating & Initializing a multimap of String & Ints
	multimap<string, int> multimapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "aaa", 12 },
			{ "ccc", 13 }
	};
	// Create a multimap iterator and point to the end of multimap
	multimap<string, int>::reverse_iterator it = multimapEx.rbegin();
	// Iterate over the multimap using Iterator till beginning.
	while (it != multimapEx.rend()) {
		// Accessing KEY from elemencpp tutorialed by it.
		string word = it->first;
		// Accessing VALUE from elemencpp tutorialed by it.
		int count = it->second;
		cout << word << " :: " << count << endl;
		// Increment the Iterator to point to next entry
		it++;
	}
	return 0;
}

Output:

Output

ddd :: 11
ccc  :: 13
aaa :: 12
aaa :: 10

In the previously mentioned example, we are employing a while loop to traverse the multimap in reverse sequence.

Since a multimap organizes its elements based on the keys in sorted order, iterating through a multimap will yield elements in the same order, which is the sorted order of keys.

Example 3

Let's see a simple example

Example

#include <map>  
#include <iostream>  
int main( )  
{  
   using namespace std;  
   multimap <int, int> m1;  
   multimap <int, int> :: iterator m1_Iter;  
   multimap <int, int> :: reverse_iterator m1_rIter;  
   multimap <int, int> :: const_reverse_iterator m1_crIter;  
   typedef pair <int, int> Int_Pair;   
   m1.insert ( Int_Pair ( 1, 10 ) );  
   m1.insert ( Int_Pair ( 2, 20 ) );  
   m1.insert ( Int_Pair ( 3, 30 ) );   
   m1_rIter = m1.rend( );  
   m1_rIter--;  
   cout << "The last element of the reversed multimap m1 is "  
        << m1_rIter -> first << "." << endl;   
   // begin can be used to start an iteration   
   // through a multimap in a forward order  
   cout << "The multimap is: ";  
   for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)  
      cout << m1_Iter -> first << " ";  
      cout << "." << endl;   
   // rbegin can be used to start an iteration   
   // throught a multimap in a reverse order  
   cout << "The reversed multimap is: ";  
   for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)  
      cout << m1_rIter -> first << " ";  
      cout << "." << endl;  
   // A multimap element can be erased by dereferencing to its key   
   m1_rIter = --m1.rend( );  
   m1.erase ( m1_rIter -> first );   
   m1_rIter = m1.rend( );  
   m1_rIter--;  
   cout << "After the erasure, the last element "  
        << "in the reversed multimap is "  
        << m1_rIter -> first << "." << endl;         
        return 0;
}

Output:

Output

The last element of the reversed multimap m1 is 1.
The multimap is: 1 2 3 .
The reversed multimap is: 3 2 1 .
After the erasure, the last element in the reversed multimap is 2.

In the example provided, the elements of the multimap are returned in the opposite order.

Example 4

Let's explore a basic illustration for arranging and determining the maximum salary:

Example

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> emp = {
                { 1000, 10},
                { 3500, 20 },
                { 4500, 30 },
                { 3000, 40 },
                { 3500, 50 }};

   cout << "Salary" << " | " << "ID" << '\n';
   cout<<"______________________\n";
  multimap<int,int>::reverse_iterator rit;
                                  for (rit=emp.rbegin(); rit!=emp.rend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';
    auto ite = emp.rbegin();
    cout << "\nHighest salary: "<< ite->first <<" \n";
    cout << "ID is: "<< ite->second << "\n";
  return 0;
  }

Output:

Output

Salary | ID
______________________
4500   |  30
3500   |  50
3500   |  20
3000   |  40
1000   |  10
Highest salary: 4500 
ID is: 30

In the previously mentioned scenario, a multimap named emp is utilized to store the salary as the key and the ID as the value. This configuration leverages the automatic sorting feature of multimaps, allowing us to easily determine the ID associated with the highest salary value.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience