How dynamically filled multidimensional vector without a priori known its dimension c++

家住魔仙堡 提交于 2019-12-11 17:21:38

问题


I'm writing a class for tensors, I need it for my work. I figure out how to create dynamic multidimensional vector using recursion, but I don't know how to stretch it to dimensional size what I need (like this vector<double> vec; vec.resize(6);). But I want to get dynamic vector and have created template class. Could someone help me with it? This is my code:

template<typename T,size_t N>
struct tensor_traits 
{ 
     using type = vector< typename tensor_traits<T,N-1>::type>; 
};
template<typename T>
struct tensor_traits<T,0> 
{ 
     using type = T; 
};



template<class T,size_t N>
class tenzor
{
protected:
    typename tensor_traits<T, N>::type data;
    vector<size_t> dimensions;
public:
    tenzor()
    {
        dimensions.resize(N);
    }

    tenzor(vector<size_t> dim)
    {
        if(dim.size() == N)

        {
            dimensions = dim;
            data.resize(dim);
        }
        else
            throw::length_error("wrong format!!");
    }

    void resize(vector<size_t> siz)
   {
    //some code`enter code here`
   }

}

来源:https://stackoverflow.com/questions/55285352/how-dynamically-filled-multidimensional-vector-without-a-priori-known-its-dimens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!