C++ multiset rbegin
The C++ multiset rbegin method is employed to retrieve a reverse iterator pointing to the final element of the multiset data structure.
A backward iterator for a multiset moves in the opposite direction, incrementing towards the first element of the multiset container until it reaches the beginning.
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 yields a reverse iterator pointing to the final element of the multiset, effectively returning the elements in reverse order.
Complexity
Constant.
Iterator validity
No changes.
Data Races
Accessing either the non-const or const versions of the multiset does not result in modifications to the container. It is considered safe to access the elements of a multiset concurrently.
Exception Safety
This function never throws exception.
Example 1
Let's explore a basic example showcasing the functionality of the 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 aforementioned example, the rbegin method is employed to yield a reverse iterator that points to the final element within the mymultiset multiset.
Since a multiset organizes its elements based on the keys' sorted order, traversing through a multiset will produce the same sequence as the sorted order of keys.
Example 2
Let's consider a basic illustration demonstrating how to traverse through the multiset backwards using a 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 previously mentioned scenario, a while loop is employed to traverse the multiset in reverse sequence, with the rbegin function setting the final element of the multiset.
Since a multiset organizes its elements based on the keys, looping through a multiset will produce the same sequence, which is the sorted order of keys.
Example 3
Let's consider a basic illustration to retrieve the initial element from 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 explore a basic illustration demonstrating the sorting and calculation of the highest scores:
#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 previously mentioned scenario, a multiset is utilized to store marks, with the marks serving as the keys. This approach leverages the automatic sorting capability of multisets, allowing us to easily determine the highest marks.