码迷,mamicode.com
首页 > 其他好文 > 详细

adjacent_difference

时间:2019-02-16 20:41:41      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:nbsp   write   tput   函数对象   NPU   计算   out   last   poi   

 /**
   *  @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

标签:nbsp   write   tput   函数对象   NPU   计算   out   last   poi   

原文地址:https://www.cnblogs.com/tianzeng/p/10389113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!