thrust

◆ replace_if() [4/4]

template<typename ForwardIterator , typename InputIterator , typename Predicate , typename T >
void thrust::replace_if ( ForwardIterator  first,
ForwardIterator  last,
InputIterator  stencil,
Predicate  pred,
const T &  new_value 
)

replace_if replaces every element in the range [first, last) for which pred(*s) returns true with new_value. That is: for every iterator i in the range [first, last), and s in the range [stencil, stencil + (last - first)), if pred(*s) is true then it performs the assignment *i = new_value.

Parameters
firstThe beginning of the sequence of interest.
lastThe end of the sequence of interest.
stencilThe beginning of the stencil sequence.
predThe predicate to test on every value of the range [first,last).
new_valueThe new value to replace elements which pred(*i) evaluates to true.
Template Parameters
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable.
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.
Predicateis a model of Predicate.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

The following code snippet demonstrates how to use replace_if to replace a device_vector's element with 0 when its corresponding stencil element is less than zero.

#include <thrust/replace.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;
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 also
http://www.sgi.com/tech/stl/replace_if.html
replace
replace_copy
replace_copy_if