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

Map Crend Function

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

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

The crend function in C++ map is employed to provide a constant iterator pointing to the end of the map in reverse order. It does not point to the last element but rather to the position after the last element, mirroring the element that comes before the first element in a non-reversed container.

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

Note: 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 provides a constreverseiterator pointing to the element that comes after the final element in the reversed container.

Example 1

Let's see a simple example for crend function.

Example

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,int> mymap;
  
  mymap['x'] = 100;
  mymap['y'] = 200;
  mymap['z'] = 300;

  // show content:
  map<char,int>::const_reverse_iterator rit;
  for (rit=mymap.crbegin(); rit!=mymap.crend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';

  return 0;
}

Output:

Output

z = 300
y = 200
x = 100

In the provided example, the crend method is employed to yield a constant reverse iterator pointing to the element succeeding the final element of the reversed container.

Since a map organizes its elements based on the keys in sorted order, the process of iterating through a map will consistently produce the same outcome, which is the sorted sequence of keys.

Example 2

Let's explore a basic illustration of iterating through a map in reverse order by employing a while loop.

Example

#include <iostream>
#include <map>
#include <string>
#include <iterator>

using namespace std;
 
int main() {
 
	// Creating & Initializing a map of String & Ints
	map<string, int> mapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "bbb", 12 },
			{ "ccc", 13 }
	};
 
	// Create a map iterator and point to the end of map
	map<string, int>::const_reverse_iterator it = mapEx.crbegin();
 
	// Iterate over the map using Iterator till beginning.
	while (it != mapEx.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
bbb :: 12	
aaa :: 10

In the previously mentioned example, a while loop is employed to iterate over the map in reverse order using const_iterate.

Since maps store elements in a sorted manner based on keys, iterating through a map will consequently display the elements in the order of keys being sorted.

Example 3

Let's see a simple example.

Example

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map 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

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

In the previous example, the map's elements were returned in a reversed order.

Example 4

Let's examine a straightforward illustration for organizing and determining the top score.

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main ()
{
  map<int,int> emp = {
                { 1000, 10},
                { 2500, 20 },
                { 4500, 30 },
                { 3000, 40 },
                { 5500, 50 }};

   cout << "Salary" << " | " << "ID" << '\n';
   cout<<"______________________\n";
   
  map<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
4500   | 30
3000   | 40
2500   | 20
1000   | 10

Highest salary: 5500 
ID is: 50

In the preceding illustration, a map named emp is created to store the salary as the value and the ID as the key. This approach leverages the automatic sorting feature in maps, 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