mercredi 5 décembre 2018

Implementing a 3D Matrix in C++ using Nested std::vector

I have a 3D Matrix with the MxNxL elements. Both M and N are known at the compile time. Whereas, L is variable and depends on user's input. But it will be provided at the beginning of the program and will never change during the life time of the program. I want to implement this Matrix in C++11 and I want to give the user the flexibility to rotate the 3D Matrix along both the the 1st and 2nd dimension of the matrix.

I would like to know what is the best and most efficient design option to implement this Matrix?

I have seen the below solution which uses std::vector. With std::vector the user can rotate any dimension using std::rotate. The solution is taken from this thread. However, vsoftco mentions that it is not good to use nested vector but rather make it linear. But since I have the requirement of rotating across dimension, having linear array will make the processing hard.

#include <vector>

#define M_SIZE 360
#define N_SIZE 180 

template<typename T>
using vec = std::vector<T>;

int main()
{
    uint16_t L;
    std::cin << L;
    vec<vec<vec<double>>> v{M_SIZE, vec<vec<double>>{N_SIZE, vec<double>{L}}};
}

Again, using dynamic c array is a possible solution but std::rotate would only work on the last dimension of the 3D Matrix.

Aucun commentaire:

Enregistrer un commentaire