map rend() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output:
c = 5
b = 3
a = 1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output:
a = 1

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C++
Practice Tags :