JimR
JimR

Reputation: 2225

3D vector constructor C++

For a 2d vector, I know I can go:

vector<vector<T>> vec;
vec = vector<vector<T>> (boardSize, vector<T>(boardSize));

But how do I do it for a 3d vector?

I tried

vector<vector<vector<T>>> vec;
vec = vector<vector<vector<T>>> (boardSize, boardSize, vector<T>(boardSize));

But it wouldn't compile. Any ideas?

Upvotes: 0

Views: 4089

Answers (2)

Probably the best thing would be not to do it. Create a class that offers the interface of a 3D vector (with some changes) and internally use a single dimension vector.

Upvotes: 0

Lyth
Lyth

Reputation: 2211

Just a guess:

vec = vector<vector<vector<T>>> (boardSize, vector<vector<T>>(boardSize, vector<T>(boardSize)));

That means, when you've declared a vector<vector<T>>, the second argument should be a vector<T>; and when you declared a vector<vector<vector<T>>>, the second argument should be a vector<vector<T>>, which in turn should be as in the first case.

Upvotes: 5

Related Questions