views:

325

answers:

1

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))) {}

};
+10  A: 

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.

Konrad Rudolph
Haha, you win the race this time. +1 :)
Johannes Schaub - litb