public member function
<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 number of elements removed.
Unlike member function forward_list::erase_after, which erases elements by their position (using an 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 an element is removed.
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).
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 << '\n';
return 0;
}
| |
Output:
mylist contains: 10 30 40 30 10
|
Complexity
Linear in container size (comparisons).
Iterator validity
Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and reference keep their validity.
Data races
The container is modified.
The elements removed are modified. Concurrently accessing or modifying other elements is safe, although iterating through the container is not.
Exception safety
If the equality comparison between elements is guaranteed to not throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if an exception is thrown, the container is left in a valid state (basic guarantee).