std::remove, std::remove_if
From Cppreference
Defined in header
<algorithm> | ||
template< class ForwardIterator, class T >
ForwardIterator remove( ForwardIterator first, ForwardIterator last, | (1) | |
template< class ForwardIterator, class UnaryPredicate >
ForwardIterator remove_if( ForwardIterator first, ForwardIterator last, | (2) | |
Removes all elements satisfying specific criteria from the range [first, last). The first version removes all elements that are equal to value, the second version removes all elements for which predicate p returns true.
Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. The elements between the old and the new ends of the range have unspecified values. Iterator to the new end of the range is returned.
Contents |
Parameters
first, last | - | the range of elements to process | |||||||||
value | - | the value of 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:
The signature does not need to have const &, but the function must not modify the objects passed to it. |
Return value
iterator to the new end of the range
Complexity
linear in the distance between first and last
Equivalent function
First version: |
---|
template<class ForwardIterator, class T> ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value) { ForwardIterator result = first; for (; first != last; ++first) if (!(*first == value)) { *result++ = *first; } } return result; } |
Second version: |
template<class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, UnaryPredicate p) { ForwardIterator result = first; for (; first != last; ++first) if (!p(*first)) { *result++ = *first; } } return result; } |
Example
This section is incomplete |
See also
| copies a range of elements omitting those that satisfy specific criteria (function template) |