Qt 3D-array with Qt-Objekts like QVector

后端 未结 2 1271
悲哀的现实
悲哀的现实 2021-01-24 20:32

How can I create a 3D-array only with Qt-Objects? The array should be a 3D-integer-array. I have tried to create a standard 3D-array on the heap. To allocate the memory on the h

2条回答
  •  时光取名叫无心
    2021-01-24 20:54

    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        int dim_0 = 3;
        int dim_1 = 3;
        int dim_2 = 3;
        int default_val = 0;
        QVector < QVector < QVector< int > > > vec(dim_0,
                                                   QVector < QVector  > (dim_1,
                                                                               QVector < int > (dim_2, default_val)));
    
        for( int i = 0; i < dim_0; i++)
        {
            for ( int j = 0; j < dim_1; j++)
            {
                for ( int k = 0; k < dim_2; k++)
                {
                    vec[i][j][k] = i*100 + j*10 + k;
                }
            }
        }
    
        qDebug() << vec;
    
        return a.exec();
    }
    

    Hope that helps.

提交回复
热议问题