Template Function thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator, BinaryFunction)¶
Function Documentation¶
-
template<typename
InputIterator, typenameOutputIterator, typenameBinaryFunction>
OutputIteratorthrust::adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryFunction binary_op) adjacent_differencecalculates the differences of adjacent elements in the range[first, last). That is,*firstis assigned to*result, and, for each iteratoriin the range[first + 1, last),binary_op(*i, *(i - 1))is assigned to*(result + (i - first)).This version of
adjacent_differenceuses the binary functionbinary_opto calculate differences.The following code snippet demonstrates how to use
adjacent_differenceto compute the sum between adjacent elements of a range.- Return
The iterator
result + (last - first)- Remark
Note that
resultis permitted to be the same iterator asfirst. This is useful for computing differences “in place”.- Parameters
first: The beginning of the input range.last: The end of the input range.result: The beginning of the output range.binary_op: The binary function used to compute differences.
- Template Parameters
InputIterator: is a model of Input Iterator, andInputIterator'svalue_typeis convertible toBinaryFunction'sfirst_argument_typeandsecond_argument_type, andInputIterator'svalue_typeis convertible to a type inOutputIterator'sset ofvalue_types.OutputIterator: is a model of Output Iterator.BinaryFunction's:result_typeis convertible to a type inOutputIterator'sset ofvalue_types.
#include <thrust/adjacent_difference.h> #include <thrust/functional.h> #include <thrust/device_vector.h> ... int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2}; thrust::device_vector<int> d_data(h_data, h_data + 8); thrust::device_vector<int> d_result(8); thrust::adjacent_difference(d_data.begin(), d_data.end(), d_result.begin(), thrust::plus<int>()); // d_result is now [1, 3, 3, 3, 3, 3, 3, 3]
- See
- See
inclusive_scan