通过两个下标访问数组成员

时间:2019-07-05 18:29:37

标签: c++

我已经将矩阵的成员存储到一维数组中。现在的问题是,我需要通过2索引符号访问这些成员,如下所示:

int matrix[2][3] = {{ 1 , 2 , 3 },{ 4, 5 , 6 }}; // this is no longer available
    int x , y;
    //Stored matrix data on array[]
    int array[] = {1,2,3,4,5,6}; // only this member available

    cout << "Insert the x index : ";
    cin>> x;
    cout << "Insert the y index : ";
    cin>> y;

因此,如何打印存储在其中的矩阵[1] [1]成员 在array []上?

1 个答案:

答案 0 :(得分:1)

像这样自己计算索引:

array[row * columnSize + column]