multimap rbegin in C++ STL
Last Updated :
18 Oct, 2018
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
#include <iostream>
#include <map>
using namespace std;
int main()
{
multimap< char , int > sample;
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));
cout << sample.rbegin()->first << " = " << sample.rbegin()->second;
}
|
Output
c = 50
Program 2
#include <iostream>
#include <map>
using namespace std;
int main()
{
multimap< char , int > sample;
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));
for ( auto it = sample.rbegin(); it != sample.rend(); it++)
cout << it->first << " = " << it->second << endl;
}
|
Output
c = 50
c = 40
b = 30
b = 20
a = 10
Share your thoughts in the comments
Please Login to comment...