reverse() Method is used to reverse a List.
Reversal of a List can be done using reverse() method. It is independent of the alphabets. And is not a sort. It is just a reversal.
#include <iostream> #include <list> using namespace std; int main() { list<string> x = {"Mohan", "Kriti", "Salim"}; x.reverse(); for (string data : x) { cout << data << endl; } return 0; }
So, in the above code we have created a List and initialised to the variable x.
list<string> x = {"Mohan", "Kriti", "Salim"};
Below is how the values are positioned in the List,
Then we have used the reverse() Method to reverse the elements of the List x.
x.reverse();
And the List x gets sorted in reverse order with Salim as the first value, Mohan second and Kriti as the third.
And we get the below output.