list reverse function in C++ STL
The list::reverse() is a built-in function in C++ STL which is used to reverse a list container. It reverses the order of elements in the list container.
Syntax:
list_name.reverse()
Parameters: This function does not accept any parameters.
Return Value: This function does not return any value. It just reverses the order of elements in the list container with which it is used.
Below program illustrate the list::reverse() function in C++ STL:
// CPP program to illustrate the // list::reverse() function #include <bits/stdc++.h> using namespace std; int main() { // Creating a list list<int> demoList; // Adding elements to the list demoList.push_back(10); demoList.push_back(20); demoList.push_back(30); demoList.push_back(40); // Initial list: cout << "Initial List: "; for (auto itr = demoList.begin(); itr != demoList.end(); itr++) cout << *itr << " "; // reversing the list demoList.reverse(); // List after reversing the order of elements cout << "\n\nList after reversing: "; for (auto itr = demoList.begin(); itr != demoList.end(); itr++) cout << *itr << " "; return 0; } |
Initial List: 10 20 30 40 List after reversing: 40 30 20 10
Recommended Posts:
- list end() function in C++ STL
- list size() function in C++ STL
- list resize() function in C++ STL
- list push_front() function in C++ STL
- list pop_front() function in C++ STL
- list back() function in C++ STL
- list max_size() function in C++ STL
- list merge() function in C++ STL
- list remove() function in C++ STL
- list front() function in C++ STL
- list erase() function in C++ STL
- list pop_back() function in C++ STL
- list empty() function in C++ STL
- list splice() function in C++ STL
- list emplace() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



