c++ - 在 C++ 中创建和传递多维数组的优雅方法?

标签 c++ pointers multidimensional-array

第一个问题:

对于已知维度,我们不需要 new/malloc 来创建

   const int row = 3;  
   const int col = 2;  
   int tst_matrix[row][col] ={{1,2},{3,4},{5,6}}

但是,要将这个二维数组传递给另一个函数并不容易,对吧?因为

   int matrix_process(int in_matrix[][])

是非法的,您必须指定除第一个维度之外的所有维度。如果我需要更改 in_matrix 的内容,我如何轻松地将 tst_matrix 传递给函数 matrix_process?

第二个问题: 使用 new 在 C++ 中创建二维数组的标准方法是什么?我不想在这里使用 std::vector 等。 这是我想出的,这是最好的方法吗?

    int **tst_arr = new int*[5];
    int i=0, j=0;

    for (i=0;i<5;i++)
    {
       tst_arr[i] = new int[5];
       for (j=0;j<5;j++)
       {
          tst_arr[i][j] = i*5+j;
       }
    }

此外,如果我将 tst_array 传递给另一个函数,例如:

     int change_row_col( int **a)       
     {
         .....................
         //check which element is 0
         for (i=0; i<5; i++)
            for(j=0;j<5;j++)
           {
             if (*(*(a+i)+j)==0)  //why I can not use a[i][j] here?
             {
               row[i]=1;
               col[j]=1;              
             }
           }
         .....................
     }

另外,如果我用((a+i)+j),结果也不是我想要的。 这是我的完整测试代码:

    #include <iostream>

    using namespace std;

    //Input Matrix--a: Array[M][N]

    int change_row_col( int **a)
    {
     int i,j;
     int* row = new int[5];
     int* col = new int[5];

     //initialization
     for(i=0;i<5;i++)
     {
        row[i]=0;
     }

     for(j=0;j<5;i++)
     {
        col[j]=0;
     }

     //check which element is 0
     for (i=0; i<5; i++)
         for(j=0;j<5;j++)
        {
           if (*(*(a+i)+j)==0)  //why I can not use a[i][j] here?
           {
               row[i]=1;
               col[j]=1;              
           }
        }

     for(i=0;i<5;i++)
       for (j=0;j<5;j++)
       {
            if (row[i] || col[j])    
            {
               *(*(a+i)+j)=0;
            }
       }
     return 1;
 }



int main ()
{
    int **tst_arr = new int*[5];
    int i=0, j=0;

    for (i=0;i<5;i++)
    {
       tst_arr[i] = new int[5];
       for (j=0;j<5;j++)
       {
          tst_arr[i][j] = i*5+j;
       }
    }

  for (i=0; i<5;i++)
  {
    for(j=0; j<5;j++)
    {
       cout<<" "<<tst_arr[i][j];
    }
    cout<<endl;
  }
 change_row_col(tst_arr);

 for (i=0; i<5;i++)
 {
     for(j=0; j<5;j++)
     {
        cout<<" "<<tst_arr[i][j];
     }
     cout<<endl;
 }

   for (i=0;i<5;i++)
   {
      delete []tst_arr[i];
   }
   delete []tst_arr;
 }

最佳答案

对于多维数组,所有边界在运行时都是可变的,据我所知,最常见的方法是使用动态分配的一维数组并“手动”进行索引计算。在 C++ 中,您通常会使用诸如 std::vector 专门化的类来管理此数组的分配和释放。

这会产生与具有固定边界的多维数组基本相同的布局,并且没有任何实际的隐含开销,因为没有固定边界,任何方法都需要在运行时传递数组维度中的所有栏。

关于c++ - 在 C++ 中创建和传递多维数组的优雅方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655149/

相关文章:

c++ - GetKeyboardLayout 返回奇怪的布局

c++ - 较小的管道如何加速数据流?

c++ - 字符指针抛出访问冲突

c++ - 将指针数组传递给函数时遇到问题

php - 如何使用 PHP 文件中的信息动态更改页面的 HTML 内容?

java - 需要一种存储 4D 数组的方法

c++ - C++中如何正确定义常量

C++: 语法错误 C2061: 标识符

c++ - 深度复制 const 指针到 const 对象

multidimensional-array - 符号识别,例如 scikit-learn 中的手写数字示例(python)