Alright, I suspect there is already a solution here somewhere on the forums but I'm not quite sure I'm searching for the right terms so I hope you dont mind me asking the question anyway.
My goal is to make a constructor for a class called building.
instance variables are:
int _id; //id of the building
int _floors; // numbers of floors
int _rooms_per_floor; //rooms per floor
std::vector<std::vector<person*>> _assignments //this represents the building
now the constructor so far looks like this:
building::building(int id_, int floors_, int rooms_per_floor_)
: _id(id_), _floors(floors_), _rooms_per_floor(rooms_per_floor_)
{
// TODO: creating the 2d vector _assignments with #_floors rows and #rooms_per_floor columns
}
The problem is how to create a 2d vector dynamically according to the number of floors and the number of rooms. If I just had a fixed number for both it would be obviously no problem, but I dont know how to approach this dynamically.
edit: if I had a fixed number for floors (lets say 3) and rooms (also 3) I'd proceed like this
std::vector<person*> floor_one;
floor_one.push_back(NULL);
floor_one.push_back(NULL);
floor_one.push_back(NULL);
std::vector<person*> floor_two;
//push 3 times with NULL
std::vector<person*> floor_three
//push 3 times with NULL
_assignments.push_back(floor_three);
_assignments.push_back(floor_two);
_assignments.push_back(floow_one)
thinking about it made me come up with this. could this work?
for (int i = 0; i<number_of_floors; ++i) {
_assignments.push_back(std::vector<person*>(_number_of_rooms);
}