0

I have a class object called Cube:

class Cube{
  public:    
    Cube();
};

Cube::Cube(){}

I create a 3D grid of the Cube objects as such:

 vector<vector<vector<Cube>>> grid;

Now I want to populate it with a certain amount of Cube objects. Essentially I want to do the same thing as if I was creating a 3D array:

Cube grid[10][10][10]

Is this possible in C++?

  • You can do it like this: https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector – Rietty Feb 04 '19 at 22:13

1 Answers1

1

Right now, you're calling the std::vector default constructor, however there's also a constructor that takes a size and item value. For the full list, see the cppreference page.

So you can actually do this:

vector<vector<vector<Cube>>> grid(10, vector<vector<Cube>>(10, vector<Cube>(10, Cube());

Which will give you a 10x10x10 3D vector filled with Cube() (default Cube) objects.

scohe001
  • 15,110
  • 2
  • 31
  • 51