mardi 23 janvier 2018

Overloading double subscript operator of std::array [duplicate]

I like to store a 3x3 Matrix as a std::array<double,9>. Furthermore, I like to overload the [][] operator, in order to access a component of the matrix like

using matrix = std::array<double,9>

matrix A {1,2,3,4,5,6,7,8,9};
double A_11 = A[1][1];

I could easily wrap my matrix in a class and write a getter method like

double Matrix::get_component(int i, int j) {
    return A[3*i+j];
}

Nevertheless, this would spoil the readability of the code. I read this answer to a similar question, but I don't know how to adapt this to a fixed size std::aray.

Aucun commentaire:

Enregistrer un commentaire