How to use std::acumulate for matrix

  accumulate, c++, c++11
#include <iostream>
#include <numeric>
#include <vector>


using matrix = std::vector<std::vector<int>>;

int main()
{
    matrix mtx{5, std::vector<int>(5)};

    int sum = 0;
    for (const auto i : mtx) // can be avoided ? 
    sum += std::accumulate(i.begin(), i.end(), 0, 
        [](int a, int b){a > 0 ? a + b : a;});

}

I want to use std::accumulate for std::vector<std::vector<int>> but I am curious if I can avoid the loop. Also I want to know if the last argument is ok.

Source: Windows Questions C++

LEAVE A COMMENT