C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ set crbegin()C++ set crbegin() function is used to return a constant reverse iterator referring to the last element in the set container. A constant reverse iterator of set moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the set container and points to the constant element. Syntaxconst_reverse_iterator crbegin() const noexcept; //since C++ 11 ParameterNone Return valueIt returns a constant reverse iterator pointing to the last element of the set. ParameterNone Return ValueIt returns a constant reverse iterator pointing to the last element of the multimap. ComplexityConstant. Iterator validityNo changes. Data racesThe container is accessed. Concurrently accessing the elements of a set is safe. Exception SafetyThis function never throws exceptions. Example 1Let's see the simple example for 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 above example, crbegin() function is used to return a constant reverse iterator pointing to the last element in the myset set. Because set stores the elements in sorted order of keys therefore, iterating over a set will result in above order i.e. sorted order of keys. Example 2Let's see a simple example to iterate over the set in reverse order using 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 element pointed 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 above example, we are using while loop to const_iterate over the set in reverse order and crbegin() function initializing the last element of the set. Because set store the elements in sorted order of keys therefore, iterating over a set will result in above order i.e. sorted order of keys. Example 3Let's see a simple example to get the first element of the reversed set: #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 above example, crbegin() function returns the first element of the reversed set s1 i.e. 40. Example 4Let's see a simple example to sort and calculate the highest marks: #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 above example, a set marks is implemented where the elements of this set are stored as keys. Function crbegin() enables us to take advantage of the auto sorting in sets, and to identify the highest marks.
Next TopicSet crend() Function
|