The third parameter of the parameter signature accumulate Function

Source: Internet
Author: User

Accumulate elements within a certain period. The accumulate function calculates the sum of Val and all elements within the range of [start, end.
If binary function f is specified, it is used for computing and operations.

#include <NUMERIC> T accumulate( input_iterator start, input_iterator end, T val ); T accumulate( input_iterator start, input_iterator end, T val, BinaryFunction f );

Computes the sum of the given value init and the elements in the range [first, last ). the first version uses operator + to sum up the elements, the second version uses the given binary function op.


#include <IOSTREAM>usingstd::cout;#include <VECTOR>usingstd::vector;#include <NUMERIC>usingstd::accumulate;  intmain() {vector<INT> v; constintSTART = 1, END = 10;for(inti = START; i <= END; ++i ) v.push_back(i);intsum = accumulate( v.begin(), v.end(), 0 );cout <<"sum from "<< START <<" to " << END << " is "<< sum <<'\n';}



What does the third parameter mean? I think it should be an initial value. It is best to write it as (INT) 0 or double (0.0). This is also verified in C ++ primer.

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: 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.