Paul Nganga
Paul Nganga

Reputation: 1

Can I create an array of different sized vectors in C++?

I am working on a program that assigns the user a parking spot in one of 9 lots arranged in a 3x3 grid. Each lot has a different capacity. I want to use vectors so I can make it shrink according to the amount of spaces left in the specific lot, and I feel it makes sense to hold the vectors in a 3-dimensional array. Is this possible in C++, and if so, how would I go about creating it?

Upvotes: 0

Views: 81

Answers (2)

flynn
flynn

Reputation: 33

I think you mean two-dimensional array. Yes, you can do that.

vector<class> lots[3][3];  // class is your datatype, and you can do better by making 3 a constant
cout << lots[1][1].size() << endl; // access vector in array

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59987

A three dimensional array using vectors is simple. e.g.

#include <vector>

using std;

vector<vector<vector<Node>>> lot; // Where Node is a datatype;
lot[x][y][z] = some value; // To write
a = lot[x][y][z]; // To read

Upvotes: 0

Related Questions