首页 > 代码库 > 泛型算法一
泛型算法一
1.accumulate:计算给定区间值的累加和
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 );
// TEMPLATE FUNCTION accumulate WITH BINOP
template<class _InIt,
class _Ty,
class _Fn2> inline
_Ty _Accumulate(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func)
{ // return sum of _Val and all in [_First, _Last), using _Func
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
for (; _First != _Last; ++_First)
_Val = _Func(_Val, *_First);
return (_Val);
}
// TEMPLATE FUNCTION accumulate
template<class _InIt,
class _Ty> inline
_Ty _Accumulate(_InIt _First, _InIt _Last, _Ty _Val)
{ // return sum of _Val and all in [_First, _Last)
_DEBUG_RANGE(_First, _Last);
for (; _First != _Last; ++_First)
_Val = _Val + *_First;
return (_Val);
}
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(""), 不能是""。
实例参考代码:
void Accumulate()
{
int sum = 0;
int x[20]={0};
for (int i = 0;i < 20;i++)
{
x[i] = i;
sum = sum + x[i];
}
cout<<"x[0..19] accumulate:"<<endl;
cout<<sum<<endl;
//用这个是因为&x[20]是作为迭代器参数传进来的,迭代器的end指向的是最后一个元素的后面
cout<<accumulate(&x[0],&x[20],0)<<endl;//这个0就是不加其它数,也可以把任何同类型数和前面迭代的内容相加
cout<<"x[1..5] accumulate:"<<endl;
//x[1..5]和10累乘
cout<<accumulate(&x[1],&x[5],10,multiplies<int>())<<endl;
}
下面介绍下上面用到的multiplies
1描述:The struct provides a predefined function object that performs the arithmetic operation of multiplication on elements of a specified value type.
2原型:
template<class Type> struct multiplies : public binary_function <Type, Type, Type> { Type operator()( const Type& _Left, const Type& _Right ) const; };
// TEMPLATE STRUCT multiplies
template<class _Ty>
struct multiplies
: public binary_function<_Ty, _Ty, _Ty>
{ // functor for operator*
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator* to operands
return (_Left * _Right);
}
}
3参数解释:
- _Left
A number that is of the parameter type Type that is to be multiplied by the function object.
- _Right
A number that is of the parameter type Type that is to be multiplied by the function object.
4返回值:
The product of the multiplication _Left * _Right.
泛型算法一