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

后端 未结 4 902
你的背包
你的背包 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:49

    Why not use std::vector?

    std::vector > array;
    

    If you don't want to use an array of pointers, you could use one large array that you allocate dynamically after you get the size and access it as an array of rows.

    int rows = 10;
    int columns = 20;
    
    int* array = new int[rows * columns];
    
    for (int count = 0; count < rows; count++)
    {
       int* row = &array[count * columns];
    
       for (int inner_count = 0; inner_count < columns; inner_count++)
       {
          int* element = &row[inner_count];
    
          //do something
       }
    }
    
    delete [] array;
    

提交回复
热议问题