English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ The List reverse() function can reverse the order of elements in the list container.
void reverse();
It does not contain any parameters.
It does not return any value.
Let's look at a simple example when the list contains integer values.
#include iostream #include<list> using namespace std; int main() { std::list<int> li={1,2,3,4,5,6}; cout << "The content of list li is:"; for(list<int>::iterator itr=li.begin();itr!=li.end();++itr) cout <<*itr; li.reverse(); cout << '\n'; cout << "After reversal, the content of list li is:"; for(list<int>::iterator itr=li.begin();itr!=li.end();++itr) cout <<*itr; cout << '\n'; return 0; }
Output:
The content of list li is: 123456 After reversal, the content of list li is: 654321
In this example, the reverse() function reverses the content of the list li, and the output is654321.
Let's look at a simple example when the list elements are strings
#include iostream #include<list> using namespace std; int main() { std::list<string> li={"mango", "is", "a", "fruit"}; cout << "The content of list li is:"; for(list<string>::iterator itr = li.begin(); itr != li.end();++itr) cout <<*itr << " \"; li.reverse(); cout << '\n'; cout << "After reversal, the content of list li is:"; for(list<string>::iterator itr = li.begin(); itr != li.end();++itr) cout <<*itr << " \"; cout << '\n'; return 0; }
Output:
The content of the list li is: 'mango is a fruit' After reversing, the content of the list li is: 'fruit a is mango'
In this example, the reverse() function reverses the list of strings and the output is 'fruit a is mango'.