C++ 2D array to 1D array

后端 未结 4 1557
春和景丽
春和景丽 2020-12-17 00:23

I am attempting to convert a 2D array to 1D. I\'m extremely new to C/C++ but I think it\'s very important to learn how to convert a 2D array to 1D. So here I am stumbling u

4条回答
  •  情深已故
    2020-12-17 01:07

    This code

    int n=0,m=0; // 2D array nRow, nCol
    int a[n][m];
    

    is invalid. First of all the dimensions shall be constant expressions and there is no sense to set them to 0.

    And the more simple way to do your task is to use pointer. For example

    int *p = b;
    
    for ( const auto &row : a )
    {
        for ( int x : row ) *p++ = x;
    }
    

提交回复
热议问题