C++ multiset crbegin
The crbegin function in C++ for multiset is employed to retrieve a constant reverse iterator pointing to the final element within the multiset container.
A reverse iterator for a multiset moves in the opposite direction, incrementing towards the first element of the container while pointing to a constant element.
Syntax
const_reverse_iterator crbegin() const noexcept; //since C++ 11
Parameter
Return value
It provides a constant reverse iterator that points to the final element within the multiset data structure.
Parameter
Return value
The crbegin function provides a constant reverse iterator that points to the final element of the multimap data structure.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Simultaneously retrieving the elements from a multiset container is considered a secure operation.
Exception Safety
This function never throws exceptions.
Example 1
Let's explore a straightforward illustration showcasing the crbegin function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset = {40,20,30,10,30,10};
cout << "mymultiset in reverse order:";
for (auto rit=mymultiset.crbegin(); rit != mymultiset.crend(); ++rit)
cout << ' ' << *rit;
cout << '\n';
return 0;
}
Output:
mymultiset in reverse order: 40 30 30 20 10 10
In the aforementioned instance, the crbegin method is employed to yield a constant reverse iterator that points to the final element within the mymultiset multiset.
Due to the fact that multisets arrange elements based on keys in a sorted manner, looping through a multiset will lead to the same sequence, which is the sorted order of keys.
Example 2
Let's explore a basic illustration of iterating through the multiset in reverse order utilizing a while loop:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a multiset of String & Ints
multiset<string> multisetEx = {"bbb", "ccc", "aaa", "bbb"};
// Create a multiset iterator and point to the end of multiset
multiset<string>::const_reverse_iterator it = multisetEx.crbegin();
// Iterate over the multiset using Iterator till beginning.
while (it != multisetEx.crend()) {
// 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:
ccc
bbb
bbb
aaa
In the previously mentioned scenario, a while loop is employed to iterate through the multiset in reverse order, with the crbegin function being utilized to initialize the final element of the multiset.
Because a multiset organizes its elements based on key values, iterating through a multiset will produce the same outcome, which is the sorted sequence of keys.
Example 3
Let's examine a basic illustration to retrieve the initial element of the inverted multiset:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> s1 = {20,40,10,30, 20};
auto ite = s1.crbegin();
cout << "The first element of the reversed multiset s1 is: ";
cout << *ite;
return 0;
}
Output:
The first element of the reversed multiset s1 is: 40
In the provided illustration, the crbegin method retrieves the initial element of the reversed multiset s1, which is 40.
Example 4
Let's explore a basic illustration for arranging and determining the top score:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> marks = {400, 220, 250, 250, 365, 220};
cout << "Marks" << '\n';
cout<<"______________________\n";
multiset<int>::const_reverse_iterator rit;
for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
cout << *rit<< '\n';
auto ite = marks.crbegin();
cout << "\nHighest Marks is: "<< *ite<<" \n";
return 0;
}
Output:
Marks
______________________
400
365
250
250
220
220
Highest Marks is: 400
In the previous illustration, a multiset named 'marks' is established to store elements as keys. By utilizing the crbegin function, we can leverage the automatic sorting feature of multisets to pinpoint the highest mark within the collection.