首页 > 代码库 > 泛型算法一
泛型算法一
1.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.
2函数原型(#include <numeric>)
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 );
3参数
- _First
An input iterator addressing the first element in the range to be summed or combined according to a specified binary operation.
- _Last
An input iterator addressing the last element in the range to be summed or combined according to a specified binary operation that is one position beyond the final element actually included in the iterated accumulation.
- _Val
An initial value to which each element is in turn added or combined with according to a specified binary operation.
- _Binary_op
string sum = accumulate(v.begin(), v.end(), string("")); v里每个string元素都连起来,注意这里必须是string(""), 不能是""。
4返回值
The sum of _Val and all the elements in the specified range for the first template function, or, for the second template function, the result of applying the binary operation specified, instead of the sum operation, to (PartialResult, *Iter), where PartialResult is the result of previous applications of the operation and Iter is an iterator pointing to an element in the range.
5例子
int sum = accumulate(vec.begin(), vec.end(), 42) sum设置为vec的元素之和再加上42.
string sum = accumulate(v.begin(), v.end(), string("")); v里每个string元素都连起来,注意这里必须是string(""), 不能是""。
泛型算法一