how to pass 2 dimensional array if both dimensions are unknown at compile time

后端 未结 5 1676
[愿得一人]
[愿得一人] 2021-01-20 16:26

what is this the correct way to pass 2 dimensional array of unknown size?

reprVectorsTree::reprVectorsTree(float tree[][], int noOfVectors, int dimensions)
<         


        
5条回答
  •  难免孤独
    2021-01-20 16:39

    First idea was to use vectors. But if you are working with a C code, pass it as a reprVectorsTree(float** tree, int noOfVectors, int dimensions).

    For your case:

    float tree[15][2] = {{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1},{2,1}};
    
    int nRows = 15;
    int nCols = 2;
    
    float** arr = new float*[nRows];
    
    for (int i = 0; i < nRows; ++i) {
         arr[i] = new float[nCols];
    }
    
    for (int i = 0; i < nRows; ++i) {
        for (int j = 0; j < nCols; ++j) {
            arr[i][j] = tree[i][j];
        }
    }
    
    reprVectorsTree *r1 = new reprVectorsTree(arr, nRows, nCols);
    

提交回复
热议问题