c++ declare an array of arrays without know the size

后端 未结 4 897
你的背包
你的背包 2021-01-18 14:18

I must declare an array of arrays or multidimensional array without know the size. I want to do something similar that I do in this cases with simple arrays:



        
4条回答
  •  时光说笑
    2021-01-18 14:25

    You're pretty much going to have to go with the loop version. You can make one slight improvement, which is to allocate one big block and then build your own int* index into it:

    int **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;
    

    This has the nice property that you can still use array[i][j] syntax for accessing the array.

提交回复
热议问题