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