C++ multiset rbegin
C++ multiset rbegin function is used to return a reverse iterator referring to the last element of the multiset container.
A reverse iterator of multiset moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multiset container.
Syntax
reverse_iterator rbegin(); //until C++ 11
const_reverse_iterator rbegin() const; //until C++ 11
reverse_iterator rbegin() noexcept; //since C++ 11
const_reverse_iterator rbegin() const noexcept; //since C++ 11
Parameter
Return value
It returns an iterator in reverse (reverse iterator) which points to the last element of the multiset.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The multiset is accessed neither the non-const nor the const versions modify the multiset container. Concurrently accessing the elements of a multiset is safe.
Exception Safety
This function never throws exception.
Example 1
Let's see the simple example for rbegin function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset= {20,10,20,40,10,30};
// show content:
cout<<"Elements are: "<<endl;
multiset<int>::reverse_iterator rit;
for (rit=mymultiset.rbegin(); rit!=mymultiset.rend(); ++rit)
cout << *rit<< '\n';
return 0;
}
Output:
Elements are:
40
30
20
20
10
10
In the above example, rbegin function is used to return a reverse iterator pointing to the last element in the mymultiset multiset.
Because multiset stores the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.
Example 2
Let's see a simple example to iterate over the multiset in reverse order using while loop:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a multiset of String
multiset<string> multisetEx = {"aaa", "ccc", "ddd", "bbb", "aaa", "bbb"};
// Create a multiset iterator and point to the end of multiset
multiset<string, int>::reverse_iterator it = multisetEx.rbegin();
// Iterate over the multiset using Iterator till beginning.
while (it != multisetEx.rend()) {
// Accessing KEY from elemencpp tutorialed by it.
string word = *it;
cout << word << endl;
// Increment the Iterator to point to next entry
it++;
}
return 0;
}
Output:
ddd
ccc
bbb
bbb
aaa
aaa
In the above example, we are using while loop to iterate over the multiset in reverse order and rbegin function initializing the last element of the multiset.
Because multiset stores the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.
Example 3
Let's see a simple example to get the first element of the reversed multiset:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
multiset <int> s1;
multiset <int>::iterator s1_Iter;
multiset <int>::reverse_iterator s1_rIter;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
s1.insert( 20 );
s1_rIter = s1.rbegin( );
cout << "The first element in the reversed multiset is "
<< *s1_rIter << "." << endl;
// begin can be used to start an iteration
// throught a multiset in a forward order
cout << "The multiset is:";
for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
cout << " " << *s1_Iter;
cout << endl;
// rbegin can be used to start an iteration
// throught a multiset in a reverse order
cout << "The reversed multiset is:";
for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
cout << " " << *s1_rIter;
cout << endl;
// A multiset element can be erased by dereferencing to its key
s1_rIter = s1.rbegin( );
s1.erase ( *s1_rIter );
s1_rIter = s1.rbegin( );
cout << "After the erasure, the first element "
<< "in the reversed multiset is "<< *s1_rIter << "." << endl;
return 0;
}
Output:
The first element in the reversed multiset is 30.
The multiset is: 10 20 20 30
The reversed multiset is: 30 20 20 10
After the erasure, the first element in the reversed multiset is 20.
Example 4
Let's see a simple example to sort and calculate the highest marks:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> marks = {410, 450, 465, 290, 410, 450};
cout << "Marks" << '\n';
cout<<"______________________\n";
multiset<int>::reverse_iterator rit;
for (rit=marks.rbegin(); rit!=marks.rend(); ++rit)
cout << *rit<< '\n';
auto ite = marks.rbegin();
cout << "\nHighest Marks is: "<< *ite <<" \n";
return 0;
}
Output:
Marks
______________________
465
450
450
410
410
290
Highest Marks is: 465
In the above example, a multiset marks is implemented where the marks are the keys. This enables us to take advantage of the auto sorting in multisets and lets us to identify the highest marks. ,