C++ Forward_list Library - remove() Function


Advertisements

Description

The C++ function std::forward_list::remove() removes element(s) from the forward_list that matches the value and reduces the size of forward_list by number of element removed.

Declaration

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

C++11

void remove (const value_type& val);

Parameters

val − Value of the element to be removed.

Return value

None

Exceptions

This member function never throws exception.

Time complexity

This member function never throws exception.

Example

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   cout << "List contents before remove operation" << endl;

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

   fl.remove(2);

   cout << "List contents after remove 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 before remove operation
1
2
2
3
3
3
4
5
List contents after remove operation
1
3
3
3
4
5
forward_list.htm
Advertisements