vector<int> a;
vector<double> b;
...
int sum_a = accumulate(a.begin(), a.end(), 0);
double sum_b = accumulate(b.begin(), b.end(), 0.0);
accumulate
is made available via #include <numeric>
- The third argument is the base value for the calculation; note that its type must match the type of value being stored in the container (hence the use of 0.0 rather than 0 in the second example)
- By default,
accumulate
does a summation, but you can specify a different operation as a fourth argument of the function - see accumulate.cpp for an example