replace_if
#
Overloads#
replace_if(exec, first, last, pred, new_value)
#
-
template<typename DerivedPolicy, typename ForwardIterator, typename Predicate, typename T>
void thrust::replace_if( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- ForwardIterator first,
- ForwardIterator last,
- Predicate pred,
- const T &new_value,
replace_if
replaces every element in the range[first, last)
for whichpred
returnstrue
withnew_value
. That is: for every iteratori
, ifpred(*i)
istrue
then it performs the assignment*i = new_value
.The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
negative elements with0
using thethrust::device
execution policy for parallelization:#include <thrust/replace.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.h> ... struct is_less_than_zero { __host__ __device__ bool operator()(int x) { return x < 0; } }; ... thrust::device_vector<int> A(4); A[0] = 1; A[1] = -3; A[2] = 2; A[3] = -1; is_less_than_zero pred; thrust::replace_if(thrust::device, A.begin(), A.end(), pred, 0); // A contains [1, 0, 2, 0]
See also
See also
See also
- Parameters:
exec – The execution policy to use for parallelization.
first – The beginning of the sequence of interest.
last – The end of the sequence of interest.
pred – The predicate to test on every value of the range
[first,last)
.new_value – The new value to replace elements which
pred(*i)
evaluates totrue
.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator,
ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator's
value_type
.
replace_if(first, last, pred, new_value)
#
-
template<typename ForwardIterator, typename Predicate, typename T>
void thrust::replace_if( - ForwardIterator first,
- ForwardIterator last,
- Predicate pred,
- const T &new_value,
replace_if
replaces every element in the range[first, last)
for whichpred
returnstrue
withnew_value
. That is: for every iteratori
, ifpred(*i)
istrue
then it performs the assignment*i = new_value
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
negative elements with0
.#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] = 1; A[1] = -3; A[2] = 2; A[3] = -1; is_less_than_zero pred; thrust::replace_if(A.begin(), A.end(), pred, 0); // A contains [1, 0, 2, 0]
See also
See also
See also
- Parameters:
first – The beginning of the sequence of interest.
last – The end of the sequence of interest.
pred – The predicate to test on every value of the range
[first,last)
.new_value – The new value to replace elements which
pred(*i)
evaluates totrue
.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator,
ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator's
value_type
.
replace_if(exec, first, last, stencil, pred, new_value)
#
-
template<typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate, typename T>
void thrust::replace_if( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- ForwardIterator first,
- ForwardIterator last,
- InputIterator stencil,
- Predicate pred,
- const T &new_value,
replace_if
replaces every element in the range[first, last)
for whichpred(*s)
returnstrue
withnew_value
. That is: for every iteratori
in the range[first, last)
, ands
in the range[stencil, stencil + (last - first))
, ifpred(*s)
istrue
then it performs the assignment*i = new_value
.The algorithm’s execution is parallelized as determined by
exec
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
element with0
when its corresponding stencil element is less than zero using thethrust::device
execution policy for parallelization:#include <thrust/replace.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.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(thrust::device, A.begin(), A.end(), S.begin(), pred, 0); // A contains [0, 20, 0, 40]
See also
See also
See also
- Parameters:
exec – The execution policy to use for parallelization.
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 which
pred(*i)
evaluates totrue
.
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
ForwardIterator – is a model of Forward Iterator, and
ForwardIterator
is mutable.InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate's
argument type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator's
value_type
.
replace_if(first, last, stencil, pred, new_value)
#
-
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 whichpred(*s)
returnstrue
withnew_value
. That is: for every iteratori
in the range[first, last)
, ands
in the range[stencil, stencil + (last - first))
, ifpred(*s)
istrue
then it performs the assignment*i = new_value
.The following code snippet demonstrates how to use
replace_if
to replace adevice_vector's
element with0
when its corresponding stencil element is less than zero.#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 also
See also
See also
- 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 which
pred(*i)
evaluates totrue
.
- Template Parameters:
ForwardIterator – is a model of Forward Iterator, and
ForwardIterator
is mutable.InputIterator – is a model of Input Iterator, and
InputIterator's
value_type
is convertible toPredicate's
argument type.Predicate – is a model of Predicate.
T – is a model of Assignable, and
T
is convertible toForwardIterator's
value_type
.