adjacent_difference算法的主要用途是计算相邻元素的差额
函数模板
//adjacent_difference.h
template <typename InputIterator, typename OutputIterator>
OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result) {
if(first == last) {
return result;
}
*result = *first;
iterator_traits<InputIterator>::value_type value = *first;
while(++first != last) {
*++result = *first - value;
value = *first;
}
return ++result;
}
template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op) {
if(first == last) {
return result;
}
*result = *first;
iterator_traits<InputIterator>::value_type value = *first;
while(++first != last) {
*++result