0

2次元を使用して1次元メモリにどのように対処しますか?(値を1次元配列として格納する値を取得するなど(Matrix::ValueAt(row, col)4x4マトリックスの場合)。Matrixfloat m[16]

class Matrix4x4
{
    private float m[16];

    float getValueAt(int row, int col)
    {
        // I want this function
    }

}
4

3 に答える 3

7

m[row * 4 + col]またはその逆。

于 2012-08-21T07:46:25.057 に答える
0

一般に:

Matrix(i,j) = m[i * 列 + j]

于 2012-08-21T08:10:08.017 に答える
0

コンパイラにそれを理解させます:

class Matrix4x4
{
private:
    union
    {
        float m[16];
        float m2[4][4];
    };
public:
    float getValueAt(int row, int col)    
    {
        return m2[row][col];
    }

    float getValueAtLinear(int i)    
    {
        return m[i];
    } 
}
于 2012-08-21T07:55:37.820 に答える