forward_list::reverse() in C++ STL

std::forward_list::reverse() is an inbuilt function in CPP STL which reverses the order of the elements present in the forward_list.

Syntax:

forwardlist_name.reverse()

Parameter: The function does not accept any parameter.

Return value: The function has no return value. It reverses the forward list.

Below program demonstrates the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// reverse() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising forward list
    forward_list<int> forward = { 10, 20, 40, 30, 70 };
  
    cout << "List elements before performing reverse operation: ";
  
    for (auto it = forward.begin(); it != forward.end(); ++it)
        cout << *it << " ";
  
    // Function that performs reverse operation
    forward.reverse();
  
    // printing elements of list
    cout << "\nList elements after performing reverse operation: ";
  
    for (auto it = forward.begin(); it != forward.end(); ++it)
        cout << *it << " ";
  
    return 0;
}

chevron_right


Output:

List elements before performing reverse operation: 10 20 40 30 70 
List elements after performing reverse operation: 70 30 40 20 10


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.