You can use std::accumulate
, like following
// v the original vector
// int n =3; // groups
for(it= v.begin(); it != v.end() ; it += n )
std::cout << std::accumulate( it, it+n, 0) << std::endl ;
Here, you need to make sure it
doesn't pass end of vector v
you can do this for appending zeros, something like this :
int zeros = n - (( v.size( ) % n ) ? ( v.size( ) % n ) : n ) ;
for( int x = 1 ; x <= zeros; ++x )
v.push_back( 0 );
And the you can finally use the above for loop with std::accumulate
Demo here