C++ - Forward List reverse() Function
The C++ forward_list::reverse function is used to reverse the order of elements of the forward_list container.
Syntax
void reverse() noexcept;
Parameters
No parameter is required.
Return Value
None.
Time Complexity
Linear i.e, Θ(n).
Example:
In the below example, the forward_list::reverse function is used reverse the order of elements of the forward_list called f_list.
#include <iostream> #include <forward_list> using namespace std; int main (){ forward_list<int> f_list{10, 20, 30, 40, 50}; forward_list<int>::iterator it; cout<<"f_list contains: "; for(it = f_list.begin(); it != f_list.end(); it++) cout<<*it<<" "; //Reverse the order of all elements of f_list f_list.reverse(); cout<<"\nf_list contains: "; for(it = f_list.begin(); it != f_list.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
f_list contains: 10 20 30 40 50 f_list contains: 50 40 30 20 10
❮ C++ - Forward List