TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set crend() Function

C++ set crend() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ set crend()

C++ set crend() function is used to return a constant iterator to the end of the set (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 that points to the constant content.

Syntax

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

Parameter

None

Return value

It returns a const_reverse_iterator 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 set is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example for 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 above example, crend() function is used to return a constant reverse iterator to the element following the last element of the reversed container.

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 2

Let'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 = {"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 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.

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

3
2
1

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

Example 4

Let'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> 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 above example, a set emp is implemented where salary is stored as key. This enables us to take advantage of the auto sorting of salary in set and lets us to identify the highest salary.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf