Generic algorithm One

Source: Internet
Author: User

1.accumulate:computes the sum of the elements in a specified range including some initial value by computing Successiv E partial sums or computes the result of successive partial results similarly obtained from using a specified binary opera tion other than the sum.

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   );
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 "".

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.