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

Multimap Crend Function

BLUF: Mastering Multimap Crend 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 Crend Function

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

The C++ multimap crend method is employed to provide a constant iterator pointing to the end of the multimap in reverse order. It references the element just before the first element in the container when not reversed.

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

An immutable iterator is an iterator that points to unchangeable data.

Syntax

Example

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

Parameter

Return value

It provides a constreverseiterator 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.

Exception safety

This function never throws exception.

Example 1

Let's examine a straightforward example for the 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 provided example, the crend method is employed to yield a constant reverse iterator pointing to the element that precedes the first element of the reversed container.

Due to the fact that a multimap organizes its elements based on the keys in sorted order, iterating through a multimap will yield the elements in the same sorted key order.

Example 2

Let's explore a basic illustration of iterating over the multimap in reverse sequence utilizing 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>::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 previous instance, we are employing a while loop to iterate constantly over the multimap in a backward manner.

Since a multimap organizes its elements based on the keys in sorted order, traversing through a multimap will yield the elements in the same sequence, which is the 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 example above, the elements of the multimap are returned in reverse sequence.

Example 4

Let's explore a basic illustration to arrange and determine the top score:

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 example provided, a multimap named emp is created with the salary as the key and the ID as the value. This setup leverages the automatic sorting feature of multimaps, allowing us to easily determine the ID associated with the highest salary.

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