Multimap Rend Function

The C++ multimap rend function is used to return an iterator to the end of the multimap (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container.

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 returns a reverse iterator to the element following the last element of the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed. Neither the const nor the non-const versions modify 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 above example, rend function is used to return a reverse iterator to the element following the last element of the reversed container.

Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap will result in above order i.e. sorted order of keys.

Example 2

Let's see a simple example to iterate over the multimap in reverse order using 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 above example, we are using while loop to iterate over the multimap in reverse order.

Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap will result in above order i.e. 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 above example, elements of multimap returned in reverse order.

Example 4

Let's see a simple example to sort and calculate the highest 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 above example, a multimap emp is implemented where the ID is being stored as value and salary as key. This enables us to take advantage of the auto sorting in multimaps and lets us to identify the ID of the element with the highest salary.

Input Required

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