优雅的方式来创建和传递多维数组在c ++? [英] elegant way to create&pass multi-dimensional array in c++?
问题描述
第一个问题:
对于已知维度,我们不需要新的/ 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?
第二个问题:
什么是标准的方式创建2二维数组在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 +
}
}
此外,如果我将tst_array传递给另一个函数,如:
int change_row_col(int ** a)
{
....... ..............
//检查哪个元素是0
for(i = 0; i <5; i ++)
for(j = 0 ; j <5; j ++)
{
if(*(*(a + i)+ j)== 0)//为什么我不能在这里使用[i] [j]
{
row [i] = 1;
col [j] = 1;
}
}
.....................
}
此外,如果我使用((a + i)+ j),结果不是我想要的。
这是完整的测试代码我有:#include< iostream>
using namespace std;
//输入矩阵 - a:Array [M] [N]
int change_row_col(int ** a)
{
int i ,j;
int * row = new int [5];
int * col = new int [5];
//初始化
for(i = 0; i <5; i ++)
{
row [i] = 0;
}
for(j = 0; j <5; i ++)
{
col [j] = 0;
}
//检查哪个元素是0
for(i = 0; i <5; i ++)
for(j = 0; j < j ++)
{
if(*(*(a + i)+ j)== 0)//为什么我不能在这里使用[i] [j]
{
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 +
}
}
for(i = 0; i <5; i ++)
{
for(j = 0; j <5; )
{
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 <<}
cout<< endl;
}
for(i = 0; i <5; i ++)
{
delete [] tst_arr [i]
}
delete [] tst_arr;
}
解决方案
模板< size_t M,size_t N&
void Fun(int(& arr)[M] [N])
{
for(int i = 0; i{
for(int j = 0; j{
/****/
}
}
}
first question:
for known dimensions, we don't need new/malloc for the creation
const int row = 3; const int col = 2; int tst_matrix[row][col] ={{1,2},{3,4},{5,6}}
however, there is no easy to pass this two-dimensional array to another function, right? because
int matrix_process(int in_matrix[][])
is illegal, you have to specify all the dimensions except the first one. if I need to change the content of in_matrix, how could I easily pass tst_matrix to the function matrix_process?
second question: what's the standard way to create 2-dimensional array in c++ with new? I dont wanna use std::vector etc.. here. here is what I come up with, is it the best way?
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; } }
In addition, if I pass tst_array to another function, like:
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; } } ..................... }
In addition, if I use ((a+i)+j), the result is not what I want. Here is the complete testing code I had:
#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; }
解决方案My solution using function template:
template<size_t M,size_t N> void Fun(int (&arr)[M][N]) { for ( int i = 0 ; i < M ; i++ ) { for ( int j = 0 ; j < N ; j++ ) { /*................*/ } } }
这篇关于优雅的方式来创建和传递多维数组在c ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!