C++ set crbegin
The crbegin function in C++ is utilized to provide a constant reverse iterator that points to the final element within the set data structure.
A reverse iterator for a set moves in the opposite direction, incrementing towards the start (first element) of the set container while pointing to the constant element.
Syntax
const_reverse_iterator crbegin() const noexcept; //since C++ 11
Parameter
Return value
It yields a fixed reverse iterator indicating the final element within the set.
Parameter
Return Value
It provides a constant reverse iterator that points to the final element in the multimap collection.
Complexity
Constant.
Iterator validity
No changes.
Data races
The container is accessed.
Simultaneously retrieving the elements of a set is considered to be a secure operation.
Exception Safety
This function never throws exceptions.
Example 1
Let's explore a straightforward example showcasing the crbegin function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset = {50,20,40,10,30};
cout << "myset in reverse order:";
for (auto rit=myset.crbegin(); rit != myset.crend(); ++rit)
cout << ' ' << *rit;
cout << '\n';
return 0;
}
Output:
myset in reverse order: 50 40 30 20 10
In the previous illustration, the crbegin method is employed to yield a constant reverse iterator that points to the final element within the myset set.
Since a set organizes its elements based on the sorted order of keys, iterating through a set will produce the same outcome, which is the sorted order of keys.
Example 2
Let's explore a basic illustration of iterating through a set in reverse order utilizing a while loop:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a set of String & Ints
set<string> setEx = {"bbb", "ccc", "aaa", "ddd"};
// Create a set iterator and point to the end of set
set<string>::const_reverse_iterator it = setEx.crbegin();
// Iterate over the set using Iterator till beginning.
while (it != setEx.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:
ddd
ccc
bbb
aaa
In the previous instance, we are employing a while loop to iterate continuously over the set in reverse sequence, with the crbegin function setting the initial value to the final element of the set.
Since sets store elements in an ordered manner based on keys, iterating through a set will produce the same sequence, which is the sorted order of keys.
Example 3
Let's examine a basic illustration to retrieve the initial element of the reversed collection:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
set<int> s1 = {20,40,10,30};
auto ite = s1.crbegin();
cout << "The first element of the reversed set s1 is: ";
cout << *ite;
return 0;
}
Output:
The first element of the reversed set s1 is: 40
In the provided illustration, the crbegin method yields the initial element of the inverted set s1, which is 40.
Example 4
Let's examine a straightforward illustration to arrange and determine the maximum score:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
set<int> marks = {400, 220, 300, 250, 365};
cout << "Marks" << " | " << "Roll Number" << '\n';
cout<<"______________________\n";
set<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 | Roll Number
______________________
400
365
300
250
220
Highest Marks is: 400
In the preceding instance, a collection called marks is utilized to store the elements as keys. The crbegin function allows us to leverage the automatic sorting feature in sets and determine the maximum marks.