C++ set rend()C++ set rend() function is used to return an 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.SyntaxParameterNone Return valueIt returns a reverse iterator to the element following the last element of the reversed container. ComplexityConstant. Iterator validityNo changes. Data RacesThe container is accessed. Neither the const nor the non-const versions modify the container. Concurrently accessing the elements of a set is safe. Exception SafetyThis function never throws exception. Example 1Let's see the simple example for rend() function: Output: Elements are : 50 40 30 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 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. 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: Output: The last element in the reversed set is 10. The set is: 10 20 30 . The reversed set is: 30 20 10 . After erase, the last element in the reversed set is 20. In the above example, elements of set returned in reverse order. Example 4Let's see a simple example to sort and calculate the highest marks: Output: Salary ______________________ 5000 4500 3000 2500 1000 Highest salary: 5000 In the above example, a set emp is implemented where the ID is being stored as value and salary as key. This enables us to take advantage of the auto sorting in sets and lets us to identify the ID of the element with the highest salary. Next TopicSet crbegin() Function |
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