public member function

std::forward_list::remove

<forward_list>
void remove ( const value_type& val );
Remove elements with specific value
Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the amount of elements removed.

Unlike member function forward_list::erase_after, which erases elements by their position (iterator), this function (forward_list::remove) removes elements by their value.

A similar function, forward_list::remove_if, exists, which allows for a condition other than an equality comparison to determine whether each element is removed.

Notice that a global algorithm function, remove, exists with a similar behavior but operating in a range (between two iterators).

Parameters

val
Value of the elements to be removed.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// remove from forward_list
#include <iostream>
#include <forward_list>

int main ()
{
  std::forward_list<int> mylist = {10, 20, 30, 40, 30, 20, 10};

  mylist.remove(20);

  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}


Output:
mylist contains: 10 30 40 30 10

Complexity

Linear in container size (comparisons).

Iterator validity

All the iterators, pointers and references to elements that have not been removed remain valid after the operation and refer to the same elements they were referring to before.

See also