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。反之亦然。

posted @ 2013-03-06 19:26  freewater  阅读(1253)  评论(0编辑  收藏  举报