C++ Forward_list Library - clear() Function



Description

The C++ function std::forward_list::clear() destroys the forward_list by removing all elements from the forward_list and sets size of forward_list to zero.

Declaration

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

C++11

void clear() 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::clear() function.

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   if (!fl.empty())
      cout << "List is non−empty before clear operation." << endl;

   fl.clear();

   if (fl.empty())
      cout << "List is empty after clear operation." << endl;

   return 0;
}

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

List is non-empty before clear operation.
List is empty after clear operation.
forward_list.htm
Advertisements