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;
}
|