c++ declare an array of arrays without know the size

后端 未结 4 946
你的背包
你的背包 2021-01-18 14:18

I must declare an array of arrays or multidimensional array without know the size. I want to do something similar that I do in this cases with simple arrays:



        
4条回答
  •  遇见更好的自我
    2021-01-18 14:51

    If you care, you can have a little bit more convenience by have a helper

    template 
    struct C3DArray
    {
        vector>> m;
        C3DArray(int size_x, int size_y, int size_z)
            : m(make(T(), size_z, size_y, size_x))
        { }
    
        template  static std::vector make(U v, size_t n) {
            return { n, std::move(v) };
        }
    
        template  static auto make(U v, size_t n, Dim... other)
            -> std::vector {
            return { n, make(v, other...) };
        }
    };
    

    This uses variadics. Use it like this:

    C3DArray arr(3,4,20);
    

提交回复
热议问题