adjacent_difference

Posted tianzeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了adjacent_difference相关的知识,希望对你有一定的参考价值。

 /**
   *  @brief  Return differences between adjacent values.
   *
   *  Computes the difference between adjacent values in the range
   *  [first,last) using operator-() and writes the result to @p __result.
   *
   *  @param  __first  Start of input range.
   *  @param  __last  End of input range.
   *  @param  __result  Output sums.
   *  @return  Iterator pointing just beyond the values written to result.
   *
   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
   *  DR 539. partial_sum and adjacent_difference should mention requirements
   */
  template<typename _InputIterator, typename _OutputIterator>
    _OutputIterator
    adjacent_difference(_InputIterator __first,
            _InputIterator __last, _OutputIterator __result)
    {
      typedef typename iterator_traits<_InputIterator>::value_type _ValueType;

      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
                                         _ValueType>)
      __glibcxx_requires_valid_range(__first, __last);

      if (__first == __last)
    return __result;
      _ValueType __value = *__first;
      *__result = __value;
      while (++__first != __last)
    {
      _ValueType __tmp = *__first;
      *++__result = __tmp - __value;
      __value = _GLIBCXX_MOVE(__tmp);
    }
      return ++__result;
    }

  /**
   *  @brief  Return differences between adjacent values.
   *
   *  Computes the difference between adjacent values in the range
   *  [__first,__last) using the function object @p __binary_op and writes the
   *  result to @p __result.
   *
   *  @param  __first  Start of input range.
   *  @param  __last  End of input range.
   *  @param  __result  Output sum.
   *  @param  __binary_op Function object.
   *  @return  Iterator pointing just beyond the values written to result.
   *
   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
   *  DR 539. partial_sum and adjacent_difference should mention requirements
   */
  template<typename _InputIterator, typename _OutputIterator,
       typename _BinaryOperation>
    _OutputIterator
    adjacent_difference(_InputIterator __first, _InputIterator __last,
            _OutputIterator __result, _BinaryOperation __binary_op)
    {
      typedef typename iterator_traits<_InputIterator>::value_type _ValueType;

      // concept requirements
      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
                                         _ValueType>)
      __glibcxx_requires_valid_range(__first, __last);

      if (__first == __last)
    return __result;
      _ValueType __value = *__first;
      *__result = __value;
      while (++__first != __last)
    {
      _ValueType __tmp = *__first;
      *++__result = __binary_op(__tmp, __value);
      __value = _GLIBCXX_MOVE(__tmp);
    }
      return ++__result;
    }

_GLIBCXX_END_NAMESPACE_ALGO
} 
  1. 如果result=first,可以就地计算元素的差,储存第一元素的值,然后储存元素的差
  2. 先将*first赋值给*result,对于[first+1,last)中的每个iterator i,第一版本(重载operator -)将*i与*(i-1)之差赋值给*(result+(i-first)),第二版本(自己重定义函数对象)是binary(*i,*(i-1))赋值给*(result+(i-first))
#include <iostream>
#include <numeric>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

class F
{
    public:
        int operator()(int i,int j)
        {
            return i*j;    
        }    
};
int main()
{
    vector<int> v{1,2,3};
    vector<int> v1(v.size());
    adjacent_difference(v.begin(),v.end(),v1.begin(),F());
    for_each(v1.begin(),v1.end(),[](int i)
    {
        cout<<i<<" ";
    });
    cout<<endl;
    return 0;
}

 

以上是关于adjacent_difference的主要内容,如果未能解决你的问题,请参考以下文章

C++ 使用adjacent_difference

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板