sum of vector c++
accumulate(a.begin(), a.end(), 0);
sum vector c++
vector<int> v{1,2,3,4,5,6,7,8,9};
int sum = 0;
//Method 1:
sum = accumulate(v.begin(), v.end(), 0);
//Method 2:
for(auto& i : v) sum+=i;
sum of vector c++
//Syntax
accumulate(first, last, sum);
accumulate(first, last, sum, myfun);
first, last : first and last elements of range
whose elements are to be added
sum : initial value of the sum
myfun : a function for performing any
specific task. For example, we can
find product of elements between
first and last.
//Example
int a[] = {5 , 10 , 15} ;
int res = accumulate(a,a+3,0); // 30
sum elements in vector c++
for (auto& n : vector)
sum_of_elems += n;
sum of vector c++
#include <numeric>
sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us