So I'm trying to create a small car park management system. What I want to do is to have users input their vehicle number and it will store it into an array. I also want other elements like check in time, a serial number, vehicle type, etc. to be stored with the vehicle number.
I want to know how I can use a multidimensional array to store all what I mentioned into the vehicle number so that when the user types their veh number again, it would show all other elements stored within that.
Sorry if I'm not making it clear enough.
CodePudding user response:
The first step is to use a struct to organize your data, e.g.:
struct Vehicle {
int id;
int serial;
VehicleType type;
};
The second question is what is the approprate container for your objects. An array is fine in general as you can easily append to it. That would then be
std::vector<Vehicle> vehicles;
However whenever you want to lookup a vehicle by its id, you will need to go through all of them to find the matching one. More efficient would be a map. There are several variants but for starters, a regular one will be fine:
std::map<int, Vehicle> vehicles;
Example insertion:
// given int vin, int serial
vehicles[vin] = {vin, serial, VehicleType::Car}
An alternative would be the std::unordered_map
.
You need to familiarize yourself with these containers so you can learn about their proper use and advantages/disadvantages. A good starter is here: How to choose between map and unordered_map?
CodePudding user response:
Sounds like you're looking for something like a map: https://www.cplusplus.com/reference/map/map/ where the key is your vehicle number and the mapped value is the vehicle's details (whatever you'd like to add there).
Edit: As an example in your case
std::map<int, vehicle> vehicleMap;
To use that vehicleMap
properly you should
- define a
struct
orclass
of typevehicle
- create the objects of type
vehicle
you want to use - add your desired details to the
vehicle
objects - insert the
vehicle
objects to the map withvehicleMap.insert(int, vehicle)
.
then you can use vehicleMap.find(<int>);
to retrieve all the details of your vehicle. : )
CodePudding user response:
You can always use maps for this purpose as such:
std::map<int, Vehicle> vehicle_data;
..and here's a simple example:
#include <iostream>
#include <string>
#include <map>
class Vehicle
{
public:
std::string name;
Vehicle(std::string _name) : name(_name) { }
};
int main()
{
std::map<int, Vehicle> vehicle_data;
vehicle_data.insert({ 1, Vehicle("vehicle name 1")});
vehicle_data.insert({ 3, Vehicle("vehicle name 3") });
vehicle_data.insert({ 2, Vehicle("vehicle name 2") });
auto it1 = vehicle_data.find({ 1 });
auto it2 = vehicle_data.find({ 2 });
auto it3 = vehicle_data.find({ 4 });
if (it1 != vehicle_data.end()) std::cout << it1->second.name << std::endl;
if (it2 != vehicle_data.end()) std::cout << it2->second.name << std::endl;
if (it3 != vehicle_data.end()) std::cout << it3->second.name << std::endl;
}
In the above code, I've inserted 3 vehicles into vehicle_data
and then I've used find()
to get the iterator pointing to the element with int value of 1
, 2
, and 4
and then printed the second element's (Vehicle's) name.