Multimap Crend Function

The C++ multimap crend function is used to return a constant 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.

A constant iterator is an iterator thacpp tutorials to constant content.

Syntax

Example

const_reverse_iterator crend() const noexcept;       //since C++ 11

Parameter

Return value

It returns a constreverseiterator to the element following the last element of the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed.

Exception safety

This function never throws exception.

Example 1

Let's see the simple example for crend function:

Example

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

Output:

Output

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

In the above example, crend function is used to return a constant 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>::const_reverse_iterator it = multimapEx.crbegin();
	// Iterate over the multimap using Iterator till beginning.
	while (it != multimapEx.crend()) {
		// 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 const_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 <iostream>
#include <map>
using namespace std;
int main(void) {
   multimap<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'a', 3},
            {'d', 4},
            {'d', 5},
            };
   cout << "Multimap contains following elements in reverse order:" << endl;
   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

Output:

Output

Multimap contains following elements in reverse order:
d = 5
d = 4
b = 2
a = 3
a = 1

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 marks:

Example

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> emp = {
                { 1000, 10},
                { 2500, 20 },
                { 3000, 30 },
                { 3000, 40 },
                { 5500, 50 }};
   cout << "Salary" << " | " << "ID" << '\n';
   cout<<"______________________\n";
  multimap<int,int>::const_reverse_iterator rit;
  for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';
    auto ite = emp.crbegin();
    cout << "\nHighest salary: "<< ite->first <<" \n";
    cout << "ID is: "<< ite->second << "\n";
  return 0;
  }

Output:

Output

Salary | ID
______________________
5500   |  50
3000   |  40
3000   |  30
2500   |  20
1000   |  10
Highest salary: 5500 
ID is: 50

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: