Home > front end >  Iterate over columns in C matrix
Iterate over columns in C matrix

Time:12-01

I want to iterate over single row and column in std::vector<std::vector<int>> matrix and get their sum.

I know that I can do this in nested loop, but here is my question. Can I use

int val_sum = 0;
std::for_each(matrix_[row].begin(),matrix_[row].end(),[&](int x) {  val_sum  = x;});

for columns and how to do that?

CodePudding user response:

The analogous way of your proposal is to iterate the matrix rows and accumulate the elements in the given column.

int val_sum = 0;
std::for_each(matrix.begin(),matrix.end(),[&](std::vector<int> &row) {  val_sum  = row[column];});

But I would still prefer to use the c 11 range-loop version

int val_sum = 0;
for ( const std::vector<int> &row : matrix )
   val_sum  = row[column];

CodePudding user response:

You can use nested std::accumulate:

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<std::vector<int>> matrix = { {1, 2, 3}, {2, 3, 4}, {3, 4, 5} };
    const int sum = std::accumulate(matrix.cbegin(), matrix.cend(), 0, [](const auto acc, const auto row) {
        return acc   std::accumulate(row.cbegin(), row.cend(), 0);
    });
    std::cout << sum;
}

CodePudding user response:

You could nest two for_each. You simply have to notice that every element of the outer for_each will be another vector<int>, a row. [Demo]

#include <algorithm>  // for_each
#include <iostream>  // cout
#include <vector>

int main()
{
    std::vector<std::vector<int>> v{ {1, 2}, {3, 4}, {5, 6} };
    std::for_each(std::cbegin(v), std::cend(v), [](auto& row) {
        std::for_each(std::cbegin(row), std::cend(row), [](auto n) {
            std::cout << n << " ";
        });
        std::cout << "\n";
    });
}

If you want to sum all the elements of the matrix, you can use accumulate instead of the inner for_each loop. [Demo]

#include <algorithm>  // for_each
#include <iostream>  // cout
#include <numeric>  // accumulate
#include <vector>

int main()
{
    int result{};
    std::vector<std::vector<int>> v{ {1, 2}, {3, 4}, {5, 6} };
    std::for_each(std::cbegin(v), std::cend(v), [&result](auto& row) {
        result  = std::accumulate(std::cbegin(row), std::cend(row), 0);
    });
    std::cout << "sum = " << result << "\n";
}

Or even with two nested accumulate. [Demo]

#include <iostream>  // cout
#include <numeric>  // accumulate
#include <vector>

int main()
{
    std::vector<std::vector<int>> v{ {1, 2}, {3, 4}, {5, 6} };
    auto result = std::accumulate(std::cbegin(v), std::cend(v), 0, [](auto total, auto& row) {
        return total   std::accumulate(std::cbegin(row), std::cend(row), 0);
    });
    std::cout << "sum = " << result << "\n";
}

If (after all, I misunderstood you from a beginning, and) you want to add all the elements of a given column. [Demo]

#include <iostream>  // cout
#include <numeric>  // accumulate
#include <vector>

int sum_column(const std::vector<std::vector<int>>& v, size_t column)
{
    return std::accumulate(std::cbegin(v), std::cend(v), 0, [&column](auto total, auto& row) {
        return total   row[column];
    });
}

int main()
{
    std::vector<std::vector<int>> v{ {1, 2}, {3, 4}, {5, 6} };
    std::cout << "sum of column 1 = " << sum_column(v, 1) << "\n";
}
  • Related