Template Function thrust::replace_if(ForwardIterator, ForwardIterator, InputIterator, Predicate, const T&)¶
Function Documentation¶
-
template<typename
ForwardIterator, typenameInputIterator, typenamePredicate, typenameT>
voidthrust::replace_if(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred, const T &new_value) replace_ifreplaces every element in the range[first, last)for whichpred(*s)returnstruewithnew_value. That is: for every iteratoriin the range[first, last), andsin the range[stencil, stencil + (last - first)), ifpred(*s)istruethen it performs the assignment*i = new_value.The following code snippet demonstrates how to use
replace_ifto replace adevice_vector'selement with0when its corresponding stencil element is less than zero.- Parameters
first: The beginning of the sequence of interest.last: The end of the sequence of interest.stencil: The beginning of the stencil sequence.pred: The predicate to test on every value of the range[first,last).new_value: The new value to replace elements whichpred(*i)evaluates totrue.
- Template Parameters
ForwardIterator: is a model of Forward Iterator, andForwardIteratoris mutable.InputIterator: is a model of Input Iterator, andInputIterator'svalue_typeis convertible toPredicate'sargument_type.Predicate: is a model of Predicate.T: is a model of Assignable, andTis convertible toForwardIterator'svalue_type.
#include <thrust/replace.h> #include <thrust/device_vector.h> struct is_less_than_zero { __host__ __device__ bool operator()(int x) { return x < 0; } }; ... thrust::device_vector<int> A(4); A[0] = 10; A[1] = 20; A[2] = 30; A[3] = 40; thrust::device_vector<int> S(4); S[0] = -1; S[1] = 0; S[2] = -1; S[3] = 0; is_less_than_zero pred; thrust::replace_if(A.begin(), A.end(), S.begin(), pred, 0); // A contains [0, 20, 0, 40]
- See
- See
replace- See
replace_copy- See
replace_copy_if