C++20 sections · 1024 units
Open in Course

accumulate

Computing totals

I'll show you accumulate(v.begin(), v.end(), 0) to sum all elements in a range. The third argument is the starting value that gets added to the sum of elements. You must include for this function since it's separate from .

Use 0LL as the initial value if you need long long to avoid integer overflow. You can provide a custom operation: accumulate(v.begin(), v.end(), 1, multiplies()) computes the product instead of sum.

Default binary operation is addition.