开发者

C++: How to make constructor for multidimensional vector?

开发者 https://www.devze.com 2022-12-20 16:38 出处:网络
I want to create two and three dimensional vectors using a constructor in a class. However, I do not know how for multidimensional vectors.

I want to create two and three dimensional vectors using a constructor in a class. However, I do not know how for multidimensional vectors.

One dimensional works:

class One{
    public:
        vector < float > myvector;

        One(int length) : myvector(length){}

};

Two dimensional does not work:

class Two{
    public:
        vector < vector < float > > myvector;

        Two(int length, int width) : myvector(length)(width) {}

};

Three dime开发者_Python百科nsional does not work either:

class Three{
    public:
        vector < vector < vector < float > > > myvector;

        Three(int length, int width, int height) : myvector(length)(width)(height) {}

};

The answer below works for two dimensional vector. I would expect the following code for three dimensional however it seems to be wrong

class Three{
    public:
        vector < vector <  vector < float > > > myvector;

        Three(int length, int width, int height) : myvector(length, vector<float>(width, vector<float>(height))) {}

};


For the twodimensional case, it should be:

Two(int length, int width) : myvector(length, std::vector<float>(width)) {}

I’ll let you figure out the third case yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消