class vehicle
{
public:
vehicle();
virtual ~vehicle();
void addVehicle();
void deleteVehicle();
void printvehicle(vehicle v);
void show();
void showleft();
void vehcileLoad();
protected:
private:
std::string pltno;
date dt;
lime arrive;
lime departure;
};
int static totalvehicle=0,totalcar=0,totalamt=0,i=0,z=0;
void vehicle::addVehicle()
{
vehicle *v = new vehicle;
std::cin.ignore();
std::cout<<"Enter vehicle number : ";
std::getline(std::cin, v->pltno);
std::cout<<"Enter arrival time in hours minutes and seconds : ";
std::cin>>v->arrive.hh>>v->arrive.col1>>v->arrive.mm>>v->arrive.col2>>v->arrive.ss;
std::cout<<"Enter date in day month and year: ";
std::cin>>v->dt.day>>v->dt.sym1>>v->dt.month>>v->dt.sym2>>v->dt.year;
veh.at(i).pltno=v->pltno;
veh.at(i).arrive.hh=v->arrive.hh;
veh.at(i).arrive.mm=v->arrive.mm;
veh.at(i).arrive.ss=v->arrive.ss;
veh.at(i).dt.day=v->dt.day;
veh.at(i).dt.month=v->dt.month;
veh.at(i).dt.year=v->dt.year;
This is part of the code i have this is also how i store data into the vector using v->pltno = ...
etc I would like to know how can i store all elements within the vector into a hash table please help i am clueless how can i store all items with veh and vehleft into a hash table 1 being for vehciles in parking and 1's which have left
what would the code look like to perform such a task?
std::vector<vehicle> veh(100);
std::vector<vehicle> vehleft(100);
Thank you?
Assuming all of your vehicle objects are stored in a std::vector<Vehicle>
, using a std::unordered_map<std::string, Vehicle> is a very simple way of getting a hash table populated with the vehicles:
#include <unordered_map>
#include <vector>
#include <string>
//...
class Vehicle
{
//...
};
void foo()
{
std::vector<Vehicle> veh(100);
//...
std::unorderd_map<std::string, Vehicle> mapVehicle;
for (auto& v : veh)
{
// add the vehicle v to the hash table
mapVehicle[v.pltNo] = v;
}
//...
}
Once you do that, you lookup a vehicle using the plate number:
Vehicle& v = mapVehicle["ABC123"]; // this is the vehicle with license plate ABC123
or to be safe (since using [] to do a search will add an empty entry if not found), you can check if the vehicle exists using std::unordered_map::find()
:
auto iter = mapVehicle.find("ABC123");
if (iter != mapVehicle.end())
{
Vehicle& v = iter->second;
}