Open In App

map rend() function in C++ STL

Last Updated : 23 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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: CPP
// C++ program to illustrate map::rend() function

#include <iostream>
#include <map>
using namespace std;

int main()
{
    map<char, int> mymap;

    // Insert pairs in the multimap
    mymap.insert(make_pair('a', 1));
    mymap.insert(make_pair('b', 3));
    mymap.insert(make_pair('c', 5));

    // Show content
    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: CPP
// C++ program to illustrate map::rend() function

#include <iostream>
#include <map>
using namespace std;

int main()
{

    map<char, int> mymap;

    // Insert pairs in the multimap
    mymap.insert(make_pair('a', 1));
    mymap.insert(make_pair('b', 3));
    mymap.insert(make_pair('c', 5));

    // Get the iterator pointing to
    // the preceding position of
    // 1st element of the map
    auto it = mymap.rend();

    // Get the iterator pointing to
    // the 1st element of the multimap
    it--;

    cout << it->first
         << " = "
         << it->second;

    return 0;
}
Output:
a = 1

Next Article
Practice Tags :

Similar Reads