使用new新建动态二维数组(多注意)


#include<iostream>
using namespace std;
int main()
{
    //设想要建立一个rows行,cols列的矩阵
    //使用new进行新建
    int rows, cols;
    cin >> rows >> cols;
    int **Array = new int*[rows];   //开辟行
    //new
    for (int i = 0; i < rows; i++)
        Array[i] = new int[cols];       //开辟列

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < cols; j++)
            cin >> Array[i][j];
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0;j < cols ; j++)
            cout << Array[i][j];
        cout << endl;
    }
    //delete
    for(int i = 0; i < rows; i++)
        delete [] Array[i];
    delete []Array;
    return 0;
}

对于其它类型的也可以写成类似的形式,很方便。

参考博客:https://blog.csdn.net/u012027907/article/details/16370625


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM