Multiset Crend Function

C++ multiset crend

C++ multiset crend function is used to return a constant iterator to the end of the multiset (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container.

Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

A constant iterator is an iterator thacpp tutorials to the constant content.

Syntax

Example

const_reverse_iterator crend() const noexcept;       //since C++ 11

Parameter

Return value

crend funtion returns a constreverseiterator to the element following the last element of the reversed container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example for crend function:

Example

#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:

Output

mymultiset in reverse order: 40 30 20 20 10

In the above example, crend function is used to return a constant reverse iterator to the element following the last element of the reversed container.

Because multiset stores the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.

Example 2

Let's see a simple example to iterate over the multiset in reverse order using while loop:

Example

#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:

Output

ccc
bbb
bbb
aaa

In the above example, we are using while loop to const_iterate over the multiset in reverse order.

Because multiset stores the elements in sorted order of keys therefore, iterating over a multiset will result in above order i.e. sorted order of keys.

Example 3

Let's see a simple example:

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:

Output

3
2
2
1

In the above example, elements of multiset returned in reverse order.

Example 4

Let's see a simple example to sort and calculate the highest marks:

Example

#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:

Output

Salary
______________________
4500
2500
2500
1200
1000

Highest salary: 4500

In the above example, a multiset emp is implemented where salary is stored as key. This enables us to take advantage of the auto sorting of salary in multiset and lets us to identify the highest salary.

Input Required

This code uses input(). Please provide values below: