◆ adjacent_difference() [4/4]

template<typename InputIterator , typename OutputIterator , typename BinaryFunction >
OutputIterator thrust::adjacent_difference ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
BinaryFunction  binary_op 

adjacent_difference calculates the differences of adjacent elements in the range [first, last). That is, *first is assigned to *result, and, for each iterator i in the range [first + 1, last), binary_op(*i, *(i - 1)) is assigned to *(result + (i - first)).

This version of adjacent_difference uses the binary function binary_op to calculate differences.

firstThe beginning of the input range.
lastThe end of the input range.
resultThe beginning of the output range.
binary_opThe binary function used to compute differences.
The iterator result + (last - first)
Template Parameters
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to BinaryFunction's first_argument_type and second_argument_type, and InputIterator's value_type is convertible to a type in OutputIterator's set of value_types.
OutputIteratoris a model of Output Iterator.
BinaryFunction'sresult_type is convertible to a type in OutputIterator's set of value_types.
Note that result is permitted to be the same iterator as first. This is useful for computing differences "in place".

The following code snippet demonstrates how to use adjacent_difference to compute the sum between adjacent elements of a range.

int h_data[8] = {1, 2, 1, 2, 1, 2, 1, 2};
thrust::device_vector<int> d_data(h_data, h_data + 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 also