The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end).
Syntax:
map_name.rend()
Parameters:The function does not take any parameter.
Return Value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the map.
Note: Reverse iterators iterate backwards i.e when they are increased they move towards the beginning of the container.
The following programs illustrates the function.
Program 1:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map< char , int > mymap;
mymap.insert(make_pair( 'a' , 1));
mymap.insert(make_pair( 'b' , 3));
mymap.insert(make_pair( 'c' , 5));
for ( auto it = mymap.rbegin(); it != mymap.rend(); it++) {
cout << it->first
<< " = "
<< it->second
<< endl;
}
return 0;
}
|
Output:
c = 5
b = 3
a = 1
Program 2:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map< char , int > mymap;
mymap.insert(make_pair( 'a' , 1));
mymap.insert(make_pair( 'b' , 3));
mymap.insert(make_pair( 'c' , 5));
auto it = mymap.rend();
it--;
cout << it->first
<< " = "
<< it->second;
return 0;
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Oct, 2018
Like Article
Save Article