TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset crbegin() function

C++ multiset crbegin() 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 crbegin()

C++ multiset crbegin() function is used to return a constant reverse iterator referring to the last element in the multiset container.

A constant reverse iterator of multiset moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multiset container and points to the constant element.

Syntax

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

Parameter

None

Return value

It returns a constant reverse iterator pointing to the last element of the multiset.

Parameter

None

Return value

crbegin() function returns a constant reverse iterator pointing to the last element of the multimap.

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 exceptions.

Example 1

Let's see the simple example for crbegin() function:

#include <iostream>
#include <set>

using namespace std;

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

  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 30 20 10 10

In the above example, crbegin() function is used to return a constant reverse iterator pointing to the last element in the mymultiset multiset.

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 = {"bbb", "ccc", "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 element pointed 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 above example, we are using while loop to const_iterate over the multiset in reverse order and crbegin() function initializing the last element of the multiset.

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 to get the first element of the reversed multiset:

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

using namespace std;

int main ()
{
  multiset<int> s1 = {20,40,10,30, 20};
          
    auto ite = s1.crbegin();
 
    cout << "The first element of the reversed multiset s1 is: ";
    cout << *ite;

  return 0;
  }  

Output:

The first element of the reversed multiset s1 is: 40

In the above example, crbegin() function returns the first element of the reversed multiset s1 i.e. 40.

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> marks = {400, 220, 250, 250, 365, 220};

   cout << "Marks" << '\n';
   cout<<"______________________\n";
   
  multiset<int>::const_reverse_iterator rit;
  for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
    cout << *rit<< '\n';

    auto ite = marks.crbegin();
 
    cout << "\nHighest Marks is: "<< *ite<<" \n";

  return 0;
  }

Output:

Marks
______________________
400
365
250
250
220
220

Highest Marks is: 400 

In the above example, a multiset 'marks' is implemented where the elements of this multiset are stored as keys. Function crbegin() enables us to take advantage of the auto sorting in multisets and lets us to identify the highest marks.

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