Thursday, May 19, 2016

Initiating Multidimensional Vectors in c++

I found this page helpful for 3D since 2D was reasonably straightforward.
http://www.cplusplus.com/forum/articles/7459/

2x1
std::vector array1D(2);

2x3
std::vector > array2D(2,std::vector(3))

2x3x4
std::vector > > array3d;
    int rows = 2, columns = 3, planes = 4;
    array3d.resize(rows);
    for (int i = 0; i < rows; i++) {
     array3d[i].resize(columns);
     for (int j = 0; j < columns; j++) {
        array3d[i][j].resize(planes);
     }
    }

and using structures
struct VoxelProperties {
    // Contains:
    int NumInVoxel; // Num points in each voxel
    std::vector NodeIndex; // Array to store the Index of points in the voxel
};
    
    std::vector Voxels(2);
    Voxels[0].NumInVoxel = 1;
    Voxels[0].NodeIndex.push_back(13);
    std::cout << Voxels[0].NumInVoxel << std::endl;
    std::cout << Voxels[0].NodeIndex[0] << std::endl;

    std::vector > Voxels(2,std::vector(3));
    Voxels[0][0].NumInVoxel = 1;
    Voxels[0][0].NodeIndex.push_back(13);
    Voxels[1][1].NumInVoxel = 11;
    Voxels[1][2].NodeIndex.push_back(15);
    std::cout << Voxels[0][0].NumInVoxel << std::endl;
    std::cout << Voxels[0][0].NodeIndex[0] << std::endl;
    std::cout << Voxels[1][1].NumInVoxel << std::endl;
    std::cout << Voxels[1][2].NodeIndex[0] << std::endl;

    std::vector > > Voxels(2,std::vector(3));
    std::vector > > Voxels(2,std::vector(2) >(3));
    std::vector > > Voxels;
    int rows = 2, columns = 3, planes = 4;
    Voxels.resize(rows);
    for (int i = 0; i < rows; i++) {
        Voxels[i].resize(columns);
    for (int j = 0; j < columns; j++) {
       Voxels[i][j].resize(planes);
    }
    }
    Voxels[0][0][0].NumInVoxel = 1;
    Voxels[0][0][1].NodeIndex.push_back(13);
    Voxels[1][1][2].NumInVoxel = 11;
    Voxels[1][2][2].NodeIndex.push_back(15);
    Voxels[1][2][2].NodeIndex.push_back(16);
    std::cout << Voxels[0][0][0].NumInVoxel << std::endl;
    std::cout << Voxels[0][0][1].NodeIndex[0] << std::endl;
    std::cout << Voxels[1][1][2].NumInVoxel << std::endl;
    std::cout << Voxels[1][2][2].NodeIndex[0] << std::endl;
    std::cout << Voxels[1][2][2].NodeIndex[1] << std::endl;

No comments: