C++ List Library - reverse() Function


Advertisements

Description

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

Declaration

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

C++98

void reverse();

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::list::reverse() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l = {1, 2, 3, 4, 5};

   l.reverse();

   cout << "List contains following elements after reverse operation" << endl;

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

   return 0;
}

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

List contains following elements after reverse operation
5
4
3
2
1
list.htm
Advertisements