#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;
}
Last modification:February 23, 2023
如果觉得我的文章对你有用,请随意赞赏