thrust::remove_if
Defined in thrust/remove.h
-
template<typename ForwardIterator, typename Predicate>
ForwardIterator thrust::remove_if(ForwardIterator first, ForwardIterator last, Predicate pred) remove_ifremoves from the range[first, last)every elementxsuch thatpred(x)istrue. That is,remove_ifreturns an iteratornew_lastsuch that the range[first,new_last)contains no elements for whichpredistrue. The iterators in the range[new_last,last)are all still dereferenceable, but the elements that they point to are unspecified.remove_ifis stable, meaning that the relative order of elements that are not removed is unchanged.The following code snippet demonstrates how to use
remove_ifto remove all even numbers from an array of integers.#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(A, A + N, is_even()); // The first three values of A are now {1, 5, 7} // Values beyond new_end are unspecified
See also
remove
See also
remove_copy
See also
remove_copy_if
Note
The meaning of “removal” is somewhat subtle.
remove_ifdoes not destroy any iterators, and does not change the distance betweenfirstandlast. (There’s no way that it could do anything of the sort.) So, for example, ifVis a device_vector,remove_if(V.begin(), V.end(), pred)does not changeV.size():Vwill contain just as many elements as it did before.remove_ifreturns 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 isS.erase(remove_if(S.begin(), S.end(), pred), S.end()).- Parameters
first – The beginning of the range of interest.
last – The end of the range of interest.
pred – A predicate to evaluate for each element of the range
[first,last). Elements for whichpredevaluates totrueare removed from the sequence.
- Template Parameters
ForwardIterator – is a model of Forward Iterator,
ForwardIteratoris mutable, andForwardIterator'svalue_typeis convertible toPredicate'sargument type.Predicate – is a model of Predicate.
- Returns
A ForwardIterator pointing to the end of the resulting range of elements for which
predevaluated totrue.