C++ multiset rbegin()C++ multiset rbegin() function is used to return a reverse iterator referring to the last element of the multiset container. A reverse iterator of multiset moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multiset container. SyntaxParameterNone Return valueIt returns an iterator in reverse (reverse iterator) which points to the last element of the multiset. ComplexityConstant. Iterator validityNo changes. Data RacesThe multiset is accessed neither the non-const nor the const versions modify the multiset container. Concurrently accessing the elements of a multiset is safe. Exception SafetyThis function never throws exception. Example 1Let's see the simple example for rbegin() function: Output: Elements are: 40 30 20 20 10 10 In the above example, rbegin() function is used to return a reverse iterator pointing to the last element in the mymultiset 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 2Let's see a simple example to iterate over the multiset in reverse order using while loop: Output: ddd ccc bbb bbb aaa aaa In the above example, we are using while loop to iterate over the multiset in reverse order and rbegin() 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 3Let's see a simple example to get the first element of the reversed multiset: Output: The first element in the reversed multiset is 30. The multiset is: 10 20 20 30 The reversed multiset is: 30 20 20 10 After the erasure, the first element in the reversed multiset is 20. Example 4Let's see a simple example to sort and calculate the highest marks: Output: Marks ______________________ 465 450 450 410 410 290 Highest Marks is: 465 In the above example, a multiset marks is implemented where the marks are the keys. This enables us to take advantage of the auto sorting in multisets and lets us to identify the highest marks. , Next TopicC++ multiset |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India