c++的两种二维数组的申请方法

大耗子 2020年02月21日 376次浏览
#include <iostream>
using namespace std;
void main()
{
    int row, col;
    cin >> row >> col;
    int**mouse = new int*[row];//行列皆可为变量
    int(*bigmouse)[4] = new int[row][4];//只可以用常量作为列数
    for (int i = 0; i < row; i++)//给mouse数组申请列
        mouse[i] = new int[col];
    for (int i = 0; i < row*col; i++)//给mouse数组赋值
        mouse[i / col][i%col] = i;
    for (int i = 0; i < row*4; i++)//给bigmouse数组赋值
        bigmouse[i / 4][i % 4] = i;
    cout << "第一种" << endl;
    for (int i = 0; i < row*col; i++)
    {
         cout << mouse[i / col][i%col]<<"   ";
         if (i%col==col-1)
             cout << endl;
    }
    cout << "第二种" << endl;
    for (int i = 0; i < row*4; i++)
    {
        cout << bigmouse[i / 4][i%4] << "   ";
        if (i%4 == 3)
            cout << endl;
    }
    for (int i = 0; i < row; i++)
    
        delete[]mouse[i];
    delete[]mouse;
    delete[]bigmouse;
    getchar();
}