◆ remove_if() [1/4]

template<typename DerivedPolicy , typename ForwardIterator , typename Predicate >
__host__ __device__ ForwardIterator thrust::remove_if ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
ForwardIterator  first,
ForwardIterator  last,
Predicate  pred 

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first,new_last) contains no elements for which pred is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

The algorithm's execution is parallelized as determined by exec.

execThe execution policy to use for parallelization.
firstThe beginning of the range of interest.
lastThe end of the range of interest.
predA predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to true are removed from the sequence.
A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
ForwardIteratoris a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use remove_if to remove all even numbers from an array of integers using the thrust::host execution policy for parallelization:

#include <thrust/remove.h>
struct is_even
__host__ __device__
bool operator()(const int x)
return (x % 2) == 0;
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
int *new_end = thrust::remove_if(thrust::host, A, A + N, is_even());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified
The meaning of "removal" is somewhat subtle. remove_if does not destroy any iterators, and does not change the distance between first and last. (There's no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove_if(V.begin(), V.end(), pred) does not change V.size(): V will contain just as many elements as it did before. remove_if returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove_if(S.begin(), S.end(), pred), S.end()).
See also