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
// 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;
} |
Output
c = 50
Program 2
// 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;
} |
Output
c = 50 c = 40 b = 30 b = 20 a = 10