简体   繁体   中英

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

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 dimensional 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM