TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset rend() function

C++ multiset rend() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset rend()

C++ multiset rend() function is used to return an 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.

Syntax

      reverse_iterator rend();                             //until C++ 11

const_reverse_iterator rend() const;                 //until C++ 11

      reverse_iterator rend() nothrow;                //since C++ 11

const_reverse_iterator rend() const nothrow;    //since C++ 11

Parameter

None

Return value

It returns a 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. Neither the constant nor the non-constant versions modify the container.

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 rend() function:

#include <iostream>
#include <set>

using namespace std;

int main () {
   multiset<int> mymultiset = {30,40,20,10,20};

   cout << "Elements are :";
   for (auto rit = mymultiset.rbegin(); rit != mymultiset.rend(); ++rit)
      cout << ' ' << *rit;
      cout << '\n';

   return 0;
}

Output:

Elements are : 40 30 20 20 10

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

Because multisets store 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:

#include <iostream>
#include <set>
#include <string>
#include <iterator>

using namespace std;
 
int main() {
 
	// Creating & Initializing a multiset of String & Ints
	multiset<string> multisetEx = {"aaa", "bbb", "ccc", "ddd", "ccc"};
 
	// Create a multiset iterator and point to the end of multiset
	multiset<string>::reverse_iterator it = multisetEx.rbegin();
 
	// Iterate over the multiset using Iterator till beginning.
	while (it != multisetEx.rend()) {
		// 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
ccc
bbb
aaa

In the above example, we are using while loop to iterate over the multiset in reverse order. Because multisets store 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:

#include <set>  
#include <iostream>  
  
int main() {  
   using namespace std; 
   
   multiset <int> s1;  
   multiset <int>::iterator s1_Iter;  
   multiset <int>::reverse_iterator s1_rIter;  

   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 ); 
   s1.insert( 10 );
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   cout << "The last element in the reversed multiset is "  
        << *s1_rIter << "." << endl;  
  
   // end can be used to terminate an iteration   
   // throught a multiset in a forward order  
   cout << "The multiset is: ";  
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )  
      cout << *s1_Iter << " ";  
   cout << "." << endl;  
  
   // rend can be used to terminate an iteration   
   // throught a multiset in a reverse order  
   cout << "The reversed multiset is: ";  
   for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )  
      cout << *s1_rIter << " ";  
   cout << "." << endl;  
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   s1.erase ( *s1_rIter );  
  
   s1_rIter = s1.rend( );  
   --s1_rIter;  
   cout << "After erase, the last element in the "  
        << "reversed multiset is " << *s1_rIter << "." << endl;  
}  

Output:

The last element in the reversed multiset is 10.
The multiset is: 10 10 20 30 .
The reversed multiset is: 30 20 10 10 .
After erase, the last element in the reversed multiset is 20.

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:

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
  multiset<int> emp = {1000,2500,4500,5000,3000,4500};

   cout << "Salary" << '\n';
   cout<<"______________________\n";
   
  multiset<int>::reverse_iterator rit;
    for (rit=emp.rbegin(); rit!=emp.rend(); ++rit)
    cout << *rit<< '\n';

    auto ite = emp.rbegin();
 
    cout << "\nHighest salary: "<< *ite <<" \n";

  return 0;
  }

Output:

Salary
______________________
5000
4500
4500
3000
2500
1000

Highest salary: 5000

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

Next TopicC++ multiset




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