Input: set = [10 20 30 70 80 90 100 40 50 60] Output: 100 90 80 70 60 50 40 30 20 10 Input: set = [1 2 3 4 5] Output: 5 4 3 2 1
To traverse a Set in reverse order, a reverse_iterator can be declared on it and it can be used to traverse the set from the last element to the first element with the help of rbegin() and rend() functions.
- Get the set.
- Declare the reverse iterator on this set.
- Traverse the set from the last element to the first element with the help of rbegin() and rend() functions.
Below is the implementation of the above approach:
Elements of Set in normal order: 10 11 12 14 15 Elements of Set in reverse order: 15 14 12 11 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to traverse a STL map in reverse direction?
- How to traverse through all values for a given key in multimap?
- Traverse through a HashSet in Java
- Traverse through a HashMap in Java
- Min number of moves to traverse entire Matrix through connected cells with equal values
- CSS | direction Property
- CSS | flex-direction Property
- HTML | DOM Style direction Property
- How to determine the direction of a jQuery scroll event?
- Different methods to reverse a string in C/C++
- Reverse an array upto a given position
- std::reverse() in C++
- Reverse individual words
- forward_list::reverse() in C++ STL
- list reverse function in C++ STL
- Reverse an ArrayList in Java
- Reverse a LinkedList in Java
- Perl | Reverse an array
- Reverse an array in Java
- Reverse elements of a Parallel Stream in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.