在不知道大小的情况下获得二维数组

时间:2022-12-20 21:34:58

I'm new to using pointers and I have a problem. I must get a 2D array of characters from the user and the user is giving me the number of rows and columns and what the array should be filled with.

我是新手使用指针,我有一个问题。我必须从用户那里获得一个2D数组字符,并且用户正在给我行数和列数以及数组应填充的内容。

    int rows,col,i,j;
    char **array;
    int *storage;
    cin >> rows >> col;
    array = new *int[rows];
    storage = new int[rows*col];
    for (int i = 0; i < rows; ++i)
        array[i] = storage + col * i;
    for(i=0;i<rows;i++){
        for(j=0;j<col;j++){
            cin>>array[i][j];}
    }

I wrote the code above for it but it doesn't work.I tried searching for such thing but I didn't find anything. can you help me to fix the error with this code please?

我为它写了上面的代码,但它不起作用。我试图搜索这样的东西,但我没有找到任何东西。你可以帮我修一下这段代码的错误吗?

1 个解决方案

#1


0  

Array is static so use vector instead i.e std:vector it is dynamic so that you can enlarge the size of the vector during runtime.

数组是静态的,所以使用向量而不是std:vector它是动态的,这样你就可以在运行时扩大向量的大小。

#1


0  

Array is static so use vector instead i.e std:vector it is dynamic so that you can enlarge the size of the vector during runtime.

数组是静态的,所以使用向量而不是std:vector它是动态的,这样你就可以在运行时扩大向量的大小。