vendredi 26 février 2016

Boost Multi_array iterating diagonally over a 2d array

Is there a way to return a view that I can iterate over of columns, in the same way as rows.

For rows:

typedef boost::multi_array_types::index_range range;
const boost::multi_array<int,2>& card;
card[0][1] = 0; 
card[0][2] = 0; 
card[0][3] = 0; 
card[0][4] = 0;

bool win = true;
// Check for row for 0
for(auto row : card) {
    // win is true for first row
    win = std::all_of(row.begin(), row.end(), [](auto i) { return i == 0; });
}

This works for columns, but instead of the classic for loop, I feel there must be a more elegant C++11 style for() loop, if only I could retrieve an iterator to a set of columns.

card[0][1] = 0; 
card[1][1] = 0; 
card[2][1] = 0; 
card[3][1] = 0;     
card[4][1] = 0;     
for (auto j = 0U; j < card.shape()[1]; j++) {
    auto col = card[boost::indices[range()][j]];
    // Win = true for 2nd column
    win = std::all_of(col.begin(), col.end(), [](auto i) { return i == 0; });
}

Aucun commentaire:

Enregistrer un commentaire