#### How to create function with input is 2D array in C++

``````int P[5][10];
for (int i=0;i<N;i++)
{
for(int j=0;j<L;j++)
{
if(random()>0.5)
P[i][j]=1;
else
P[i][j]=0;
}
}
``````

``````void show_P(int P[][], int numcols,int numrows)
``````

5 个回复

``````void show_P(int (&P)[5][10])
``````

``````template <size_t N, size_t M>
void show_P(int (&P)[N][M])
``````

``````void show_P(int** P, int numcols, int numrows)
``````

``````void printArray(array<array<int,2>,3>& arr)
{
for (auto x : arr)
{
for (auto y : x)
{
cout << y << endl;
}
}
}

int main()
{
array<array<int,2>,3> arr{{{1, 2}, {2, 3}, {3, 4}}};
arr[0][1] = 5;
printArray(arr);
}
``````

1个

5

2

3

3

4

``````void print( int (&ref)[5][10]) {

for( auto &lm: ref) // get the first array from the multidimensional array and initialize lm with it
for( auto &elem: lm) // get the first element from lm
std::cout << elem << " "; // print the element

}
``````

``````N=5;
L=10;
void show_P( int ( &P )[N][L] )
``````
1 如何创建2d数组c ++？

2011-10-30 09:30:38 5 8940   c++/ 2d
3 如何用Matlab矩阵输入形成C ++ 2D数组？

2014-02-11 17:48:22 0 1177   c++/ mex
5 C ++中用于用户输入的2D数组

7 用C ++输入2D数组

8 C++ 2D 数组输入验证

2021-01-27 15:06:47 1 38   c++