1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <vector>
#include <numeric>
std::vector<int> partial_sums( const std::vector<int>& vec )
{
std::vector<int> result( vec.size() ) ;
std::partial_sum( vec.begin(), vec.end(), result.begin() ) ;
return result ;
}
int main()
{
const std::vector< std::vector<int> > data
{
{ 0, 3, 6, 9, 7, 5, 0 },
{ 0, 4, 12, 8, 10, 3, 0 },
{ 0, 12, 11, 6, 4, 7, 0 }
};
std::vector< std::vector<int> > sums ;
for( const auto& row : data ) sums.push_back( partial_sums(row) ) ;
for( const auto& row : sums )
{
for( int s : row ) std::cout << s << ' ' ;
std::cout << '\n' ;
}
}
|