how to calculate sum of a vector in c++ stl code example
Example 1: sum of vector c++
accumulate(a.begin(), a.end(), 0);
Example 2: sum elements in vector c++
for (auto& n : vector)
sum_of_elems += n;
Example 3: C++ sum a vector of digits
// Sum digits in vector
int digit_sum(vector<int> num) {
int sum = 0;
for (auto x : num) sum += x;
return sum;
}