I am trying to create a 2D vector of shared_ptr's. I have the following three classes:
class Spot{
bool filled = false;
};
class SpotGrid{
std::map<std::string, std::vector<std::vector<std::shared_ptr<Spot>>>> grid;//the key here is the name of the center and the value is a grid of spots.
}
class Vehicle{
std::string vehicle_name, vehicle_id;
std::set<std::string> vehicle_weekday;
};
Basically, the SpotGrid class represents a 2D structure at a particular center. For example, let's say the center at Illinois have 6 rows and 10 columns where each vehicle can be parked or we can have a center at California whose gird has 5 rows and 5 columns where vehicles can be parked. The Spot class represents each spot of the grid. And the vehicle class represents a single vehicle. Now the vehicle class have a set that represents the collection of days that it wants to find a parking at a particular center. As you can see the same spot is shared to different vehicles depending on the availability and the day of the week. For example, lets say at center Illinois which has a 6 rows and 10 columns grid, the spot (2,3) is empty on Monday. So a vehicle can park there. Similarly other spots can be filled. Now lets say on tuesday again the same spot (2,3) is empty and so a vehicle can be parked. So the same spot is shared between vehicles depending on the day and availability of the spot. So the vehicle class holds a shared_ptr to the corresponding spot on the corresponding day of the week. So in the above example, the vehicle has a pointer corresponding to each member of its vehicle_weekday
that points towards the spot. Note that this is just a simplified example. And the actual project consists of a lot more classes. My problem is that i am unable to create a vector<vector<shared_ptr<Spot>>>
to which a vehicle can point. That is i want a 2D grid of Spot corresponding to each center(like Illinois, California etc). Now after creating each of the grid i want different pointers to point to any of the particular spots. And i don't want to use new
expression since managing those will be very difficult. Is there a way to achieve this? Note that each spot is an independent entity. And each vehicle is an independent entity. But the same vehicle can point to the same spot or a different spot. For example, Vehicle car can point to spot (2,3) of the grid at Illinois on Monday, and the same vehicle car can point to spot (5,2) of the grid at California on Tuesday. Similarly another vehicle truck can point to spot(2,3) on Tuesday of the grid at Illinois and (5,2) of the grid at California on Monday. I want to allocate dynamic memory that to which different entities can point.
Aucun commentaire:
Enregistrer un commentaire