Generic algorithm One

Source: Internet
Author: User

1.accumulate: Calculates the summation of the given interval value and

2. Function prototypes (#include <numeric>)

Template<class Inputiterator, class type>    type accumulate (      _first      _last type _val          ); Template<class Inputiterator, Class Type, class binaryoperation>    Type Accumulate (      _first  _last             Type _val      _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 parameters
_first

An-input iterator addressing the first element in the range to being summed or combined according to a specified binary opera tion.

_last

An input iterator addressing, the last element in the range, is summed or combined according to a specified binary Operat Ion that's one position beyond the final element actually included in the iterated accumulation.

_val

An initial value to which each element was in turn added or combined with according to a specified binary operation.

_binary_op

String sum = Accumulate (V.begin (), V.end (), String ("")); Each string element in V is linked, note that it must be a string ("") and cannot be "".

4 return value

The sum of _val and all the elements in the specified range for the first template function, or, for the second template F Unction, 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 are an iterator point ing to a element in the range.

5 examples

int sum = accumulate (Vec.begin (), Vec.end (), sum) sums the elements of the VEC and adds 42.

String sum = Accumulate (V.begin (), V.end (), String ("")); Each string element in V is linked, note that it must be a string ("") and cannot be "".

Example Reference code:

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;
This is because &X[20] is passed in as an iterator parameter, and the end of the iterator points to the back of the last element
Cout<<accumulate (&x[0],&x[20],0) <<endl;//This 0 is no other number, you can also add any of the same type number and the contents of the previous iteration

cout<< "x[1..5] Accumulate:" <<endl;
X[1..5] and 10 multiplicative
Cout<<accumulate (&x[1],&x[5],10,multiplies<int> ()) <<endl;
}

Below is a description of the multiplies used above

1 Description: The struct provides a predefined function object that performs the arithmetic operation of multiplication on elements of a specified value type.

2 Prototypes:

  template<class type>   struct multiplies:public binary_function <type, Type, TYP E>   {  Type operator () (  const type&  _left ,   Const type&  _right ) const;  };  

       //TEMPLATE STRUCT multiplies

    struct multiplies
       : Public binary_function<_ty, _Ty , _ty>
    _ty operator () (const _ty& _left, const _ty& _ right) const
        {    /Apply operator* to operands
         Return (_left * _right);
       }
   }


3 Parameter explanation:
_left

A number, which is the parameter type type, that is, is multiplied by the function object.

_right

A number, which is the parameter type type, that is, is multiplied by the function object.

4 return Value:

The product of the multiplication _left * _right.

Generic algorithm One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.