thrust::adjacent_difference
Defined in thrust/adjacent_difference.h
-
template<typename InputIterator, typename OutputIterator>
OutputIterator thrust::adjacent_difference(InputIterator first, InputIterator last, OutputIterator result) adjacent_difference
calculates the differences of adjacent elements in the range[first, last)
. That is,*first
is assigned to*result
, and, for each iteratori
in the range[first + 1, last)
, the difference of*i
and*(i - 1)
is assigned to*(result + (i - first))
.This version of
adjacent_difference
usesoperator-
to calculate differences.The following code snippet demonstrates how to use
adjacent_difference
to compute the difference between adjacent elements of a range.Remark
Note that
result
is permitted to be the same iterator asfirst
. This is useful for computing differences “in place”.#include <thrust/adjacent_difference.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()); // d_result is now [1, 1, -1, 1, -1, 1, -1, 1]
See also
inclusive_scan
- Parameters
first – The beginning of the input range.
last – The end of the input range.
result – The beginning of the output range.
- Template Parameters
InputIterator – is a model of Input Iterator, and
x
andy
are objects ofInputIterator's
value_type
, thenx
-is
defined, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
, and the return type ofx - y
is convertible to a type inOutputIterator's
set ofvalue_types
.OutputIterator – is a model of Output Iterator.
- Returns
The iterator
result + (last - first)