C++ Forward_list Library - reverse() Function


Advertisements

Description

The C++ function std::forward_list::reverse() reverse the order of the elements present in the forward_list.

Declaration

Following is the declaration for std::forward_list::reverse() function form std::forward_list header.

C++11

void reverse() noexcept;

Parameters

None

Return value

None

Exceptions

This member function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::forward_list::reverse() function.

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 3, 4, 5};;

   fl.reverse();

   cout << "List contents after reverse operation" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

List contents after reverse operation
5
4
3
2
1
forward_list.htm
Advertisements