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++?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.