C++ multiset crend
C++ multiset crend method is employed to provide a constant iterator pointing to the end of the multiset in reverse order. It does not point to the last element but rather to the element before the first one in the non-reversed container.
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 refers to constant data.
Syntax
const_reverse_iterator crend() const noexcept; //since C++ 11
Parameter
Return value
The crend function provides a constreverseiterator pointing to the element after the final element of the reversed container.
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 exception.
Example 1
Let's explore a straightforward example demonstrating the usage of the crend function:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset = {40,20,30,10,20};
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 20 20 10
In the previous instance, the crend method is employed to provide a constant reverse iterator pointing to the element that precedes the first element in the reversed container.
Since a multiset organizes its elements based on key values in a sorted manner, iterating through a multiset will produce the same sequence, which is the sorted order of keys.
Example 2
Let's examine a basic illustration for iterating through the multiset in reverse sequence using 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 = {"ccc", "bbb", "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 example, we are utilizing a while loop to iterate const_iterate over the multiset in a reverse sequence.
Because a multiset organizes its elements based on the keys in sorted order, looping through a multiset 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()
{
multiset<int> c = {3, 1, 2, 2};
for_each(c.crbegin(), c.crend(), [](const int& x) {
cout << x << endl;
});
}
Output:
3
2
2
1
In the previous illustration, the items of the multiset are presented in the opposite sequence.
Example 4
Let's examine a basic illustration for arranging and determining the top score:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> emp = {1000,2500,4500,1200,2500};
cout << "Salary"<< '\n';
cout<<"______________________\n";
multiset<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
2500
2500
1200
1000
Highest salary: 4500
In the example provided, a multiset named emp is utilized to store salaries as keys. This functionality allows us to benefit from the automatic sorting of salaries within the multiset, facilitating the identification of the highest salary.