W3cubDocs

/C++

std::forward_list::remove, remove_if

void remove( const T& value );
(since C++11)
(until C++20)
size_type remove( const T& value );
(since C++20)
template< class UnaryPredicate >
void remove_if( UnaryPredicate p );
(since C++11)
(until C++20)
template< class UnaryPredicate >
size_type remove_if( UnaryPredicate p );
(since C++20)

Removes all elements satisfying specific criteria. The first version removes all elements that are equal to value, the second version removes all elements for which predicate p returns true.

Parameters

value - value of the elements to remove
p - unary predicate which returns ​true if the element should be removed.

The signature of the predicate function should be equivalent to the following:

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type forward_list<T,Allocator>::const_iterator can be dereferenced and then implicitly converted to Type. ​

Return value

(none).

(until C++20)

The number of elements removed.

(since C++20)

Complexity

Linear in the size of the container.

Example

#include <forward_list>
#include <iostream>
 
int main()
{
    std::forward_list<int> l = { 1,100,2,3,10,1,11,-1,12 };
 
    l.remove(1); // remove both elements equal to 1
    l.remove_if([](int n){ return n > 10; }); // remove all elements greater than 10
 
    for (int n : l) {
        std::cout << n << ' '; 
    }
    std::cout << '\n';
}

Output:

2 3 10 -1

See also

removes elements satisfying specific criteria
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/container/forward_list/remove