#include<iostream>
using namespace std;
int main() {
int row, col;
cin >> row >> col;
int** arr = new int* [row];//为二维数组分配空间
for (int i = 0; i < row; i++)
arr[i] = new int[col];//arr[i]是一个一维数组
// 输入元素
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> arr[i][j];
}
}
// delete释放二维数组
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
delete[]arr[i];
}
delete[]arr;
}
仿照上面方法
动态分配一维数组
#include<iostream>
using namespace std;
int main() {
int row;
cin >> row;
int* arr = new int[row];//分配空间
for (int i = 0; i < row; i++) {
cin >> arr[i];
}
for (int i = 0; i < row; i++) {
cout << arr[i] << " ";
}
delete[] arr;
}
One comment
畅玩传奇世界仿古版手游,私服体验火爆招募中!:https://501h.com/jingpin/2024-07-30/24376.html