C++ set crend
The C++ set crend function is employed to provide a constant iterator pointing to the end of the set in reverse order, which is not the last element but the one past the last element. It is akin to the element that comes before the first element in a container that is not reversed.
Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.
An unchanging iterator is a type of iterator that points to data that cannot be modified.
Syntax
const_reverse_iterator crend() const noexcept; //since C++ 11
Parameter
Return value
It provides a constreverseiterator pointing to the element subsequent to the final element in the reversed container.
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 exception.
Example 1
Let's explore a basic example demonstrating the usage of the crend function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset = {40,20,50,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 example provided, the crend method is employed to yield a constant reverse iterator pointing to the element that precedes the first element of the reversed container.
Due to the fact that sets organize elements based on key values in a sorted manner, iterating through a set will produce the same order, which is the sorted order of keys.
Example 2
Let's explore a basic illustration of iterating through a set in reverse order using 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 = {"ccc", "ddd", "aaa", "bbb"};
// 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 example, we are utilizing a while loop to iterate over the set in reverse order.
Since sets organize elements based on the sorted order of keys, iterating through a set will produce the same sequence, which is the sorted order of keys.
Example 3
Let's see a simple example:
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
set<int> c = {3, 1, 2};
for_each(c.crbegin(), c.crend(), [](const int& x) {
cout << x << endl;
});
}
Output:
In the example provided, the elements of the set are returned in reverse sequence.
Example 4
Let's explore a basic illustration demonstrating the process of arranging and determining the top score:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
set<int> emp = {1000,2500,4500,1200,3000};
cout << "Salary"<< '\n';
cout<<"______________________\n";
set<int>::const_reverse_iterator rit;
for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
cout << *rit<< '\n';
auto ite = emp.crbegin();
cout << "\nHighest salary: "<< *ite <<" \n";
return 0;
}
Output:
Salary
______________________
4500
3000
2500
1200
1000
Highest salary: 4500
In the scenario described, a set named emp is utilized to store salaries as keys. This allows us to benefit from the automatic sorting of salaries in the set, facilitating the identification of the highest salary.