map crbegin() and crend() function in C++ STL


In this article we will be discussing the working, syntax and examples of map::crbegin() and map::crend() functions in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map::cbegin()?

map::crbegin() function is an inbuilt function in C++ STL, which is defined in  header file. crbegin() implies constant reverse begin, means the reverse of the cbegin which was constant begin, in other words the function crbegin() will return the iterator which is pointing to the last element of the map container associated with the function. This iterator can’t be used to modify the map. This can be just used to traverse the set container.

Syntax

Map_name.crbegin();

Parameter

This function doesn’t accept any parameter.

Return value

This function returns the iterator which is pointing to the last element of the map container.

Example

Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.crbegin();

Output

c:3

map::crbegin

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_Map;
   TP_Map.insert({3, 50});
   TP_Map.insert({2, 30});
   TP_Map.insert({1, 10});
   TP_Map.insert({4, 70});
   //using map::crbegin to fetch first last element
   auto temp = TP_Map.crbegin();
   cout<<"First element is: "<<temp->first << " -> " << temp->second;
   cout<<"\nTP Map is : \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.crbegin(); i!= TP_Map.crend(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

First element is: 4 -> 70
TP Map is:
MAP_KEY    MAP_ELEMENT
4             70
3             50
2             30
1             10

What is map::crend()?

map::crend() function is an inbuilt function in C++ STL, which is defined in <map> header file. crend() implies constant reverse end iterator, means it is reverse of the cend which was constant end iterator, in other words the function crend() will return the iterator which is pointing to the position just before the first position of the map container associated with the function. This iterator can’t be used to modify the map. This can be just used to traverse the map container.

Syntax

newmap.crend();

Parameters

This function accepts no parameter.

Return value

It returns an iterator pointing to the preceding first element of the associated map container.

Example

Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.crend();

Output

error

map::crend

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_Map;
   TP_Map.insert({3, 50});
   TP_Map.insert({2, 30});
   TP_Map.insert({1, 10});
   TP_Map.insert({4, 70});
   cout<<"\nTP Map is : \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.crbegin(); i!= TP_Map.crend(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

TP Map is:
MAP_KEY    MAP_ELEMENT
4             70
3             50
2             30
1             10

Updated on: 15-Apr-2020

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements