multimap rbegin in C++ STL

multimap::rbegin() is a built-in-function in C++ STL which returns an iterator pointing to the last element of the container.
Syntax:

multimap_name.rbegiin()

Parameters: The function does not take any parameter.
Return Value: The function returns a reverse iterator pointing to the last element of the container.
(Note: Reverse iterators iterate backwards i.e when they are increased they move towards the beginning of the container)

The following two programs illustrates the function.

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// multimap::rbegin()
  
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
    multimap<char, int> sample;
  
    // Insert pairs in the multimap
    sample.insert(make_pair('a', 10));
    sample.insert(make_pair('b', 20));
    sample.insert(make_pair('b', 30));
    sample.insert(make_pair('c', 40));
    sample.insert(make_pair('c', 50));
  
    // Get the last element by
    // multimap::rbegin()
    cout << sample.rbegin()->first << " = " << sample.rbegin()->second;
}

chevron_right


Output

c = 50

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// multimap::rbegin()
  
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
    multimap<char, int> sample;
  
    // Insert pairs in the multimap
    sample.insert(make_pair('a', 10));
    sample.insert(make_pair('b', 20));
    sample.insert(make_pair('b', 30));
    sample.insert(make_pair('c', 40));
    sample.insert(make_pair('c', 50));
  
    // Show content of the multimap
    for (auto it = sample.rbegin(); it != sample.rend(); it++)
        cout << it->first << " = " << it->second << endl;
}

chevron_right


Output

c = 50
c = 40
b = 30
b = 20
a = 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.