-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic2DArray.cpp
53 lines (49 loc) · 972 Bytes
/
Dynamic2DArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
int ** make2DArray(int row, int col)
{
int ** p = new int*[row];
if(!p) return p;
for(int i=0; i<row; i++)
{
*(p+i)= new int[col];
if(*(p+i)==NULL)
return p;
}
return p;
}
void destroy2DArray(int **p, int row)
{
for(int i=0; i<row; i++)
{
delete *(p+i);
*(p+i) = NULL;
}
delete p;
p = NULL;
cout << "二维数组已销毁。" << endl;
}
int main()
{
int row, col;
cout << "请输入二维数组的行与列:";
cin >> row;
cin >> col;
int **p = make2DArray(row, col);
cout << "请输入数值:";
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
{
cin >> *(*(p+i)+j);
}
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout << *(*(p+i)+j) <<' ';
}
cout <<endl;
}
destroy2DArray(p, row);
return 0;
}