n-dimensional vector

后端 未结 1 1423
时光取名叫无心
时光取名叫无心 2021-02-05 16:27

Let\'s say I want to declare a vector of a vector of a vector of a... (up to n dimensions).

Like so:

using namespace std;
// for n=2
vector

        
相关标签:
1条回答
  • 2021-02-05 16:49

    Yes, and it's pretty straightforward.

    Much like a proof by induction, we set up a recursive case and a (partially specialized) base case that ends the recursion.

    template<size_t dimcount, typename T>
    struct multidimensional_vector
    {
        typedef std::vector< typename multidimensional_vector<dimcount-1, T>::type > type;
    };
    
    template<typename T>
    struct multidimensional_vector<0,T>
    {
        typedef T type;
    };
    
    multidimensional_vector<1, int>::type v;
    multidimensional_vector<2, int>::type v2;
    multidimensional_vector<3, int>::type v3;
    multidimensional_vector<4, int>::type v4;
    
    0 讨论(0)
提交回复
热议问题