C++ List Library - remove() Function



Description

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

Declaration

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

C++98

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

Linear i.e. O(n)

Example

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

#include <iostream>
#include <list>

using namespace std;

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

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

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

   l.remove(3);

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