STL数值算法<numeric>

<numeric>:Defines container template functions that perform algorithms provided for numerical processing.

 

accumulate

Computes the sum of all the elements in a specified range including some initial value by computing successive partial sums or computes the result of successive partial results similarly obtained from using a specified binary operation other than the sum.

 

template<class InputIterator, class Type>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val
   );

template<class InputIterator, class Type, class BinaryOperation>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val, 
      BinaryOperation _Binary_op
   );

 

inner_product

Computes the sum of the element-wise product of two ranges and adds it to a specified initial value or computes the result of a generalized procedure where the sum and product binary operations are replaced by other specified binary operations.

 

template<class InputIterator1, class InputIterator2, class Type>
   Type inner_product(
      InputIterator1 _First1, 
      InputIterator1 _Last1,
      InputIterator2 _First2, 
      Type _Val
   );

template<class InputIterator1, class InputIterator2, class Type,
   class BinaryOperation1, class BinaryOperation2>
   Type inner_product(
      InputIterator1 _First1, 
      InputIterator1 _Last1,
      InputIterator2 _First2, 
      Type _Val, 
      BinaryOperation1 _Binary_op1, 
      BinaryOperation2 _Binary_op2
   );

注:第二个序列的个数必须大于等于第一个序列的个数。因为,inner_product内部实现是以第一序列的元素个数为依据的,将两个序列都走了一遍。

可能实现为while( _First1!=_Last1){...}或者n=_Last1-_First1;while(n>0){...}

 

adjacent_difference

Computes the successive differences between each element and its predecessor in an input range and outputs the results to a destination range or computes the result of a generalized procedure where the difference operation is replaced by another, specified binary operation.

template<class InputIterator, class OutIterator>
   OutputIterator adjacent_difference(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result 
   );

template<class InputIterator, class OutIterator, class BinaryOperation>
   OutputIterator adjacent_difference(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result, 
      BinaryOperation _Binary_op
   );

 注意,可以采用就地(in place)运算方式,也就是令result等于first。当让在这种情况下它是一个质变算法(mutating algorithm)。

质变算法(mutating algorithm)会改变操作对象的值。而非质变算法(nonmutating algorithm)不会改变操作对象的值。

 

partial_sum

Computes a series of sums in an input range from the first element through the ith element and stores the result of each such sum in the ith element of a destination range or computes the result of a generalized procedure where the sum operation is replaced by another specified binary operation.

 

template<class InputIterator, class OutIt>
   OutputIterator partial_sum(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result
   );

template<class InputIterator, class OutIt, class Fn2>
   OutputIterator partial_sum(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Result, 
      BinaryOperation _Binary_op
   );

partial_sum与先前介绍的adjacent_difference互为逆运算。如果对区间值1,2,3,4,5执行partial_sum,获得结果为1,3,6,10,15,再对此结果执行adjacent_difference,便会获得原始区间值1,2,3,4,5。反之亦然。

转载于:https://www.cnblogs.com/freewater/archive/2013/03/06/2946464.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值