简体   繁体   中英

c++ struct of vector of struct

Im trying to get a better understanding of structs and I could use some help. Im trying to create a struct which contains a vector of structs, but Im having trouble linking them together. So far I got:

struct Vehicle{
    string LicensePlate;
    string VehicleType;
    vector<string> DamageType;
    int EstFixDays;
}veh;

struct DailyCustomers{
    string date;
    struct Vehicle vehicleList;
}dlc;

int main(){
  vector<Vehicle> vehicleList;
  struct DailyCustomers dlc;
  dlc.date = fileVector[1];
  for (int i = 1; i < fileVector.size(); i++){
      stringDevide(fileVector[i]);
      struct Vehicle veh;
      veh.LicensePlate = licensePlate;
      veh.VehicleType = vehicleType;
      veh.DamageType = vehicleDamageType;  
      veh.EstFixDays = est; 
      vehicleList.push_back(veh);
  }
  dlc.vehicleList = veh;
}

My program seems to be working fine, as the struct linking doesnt affect it, but Im not sure if I linked them together correctly. Should the last line be inside the for loop?

Any help greatly appreciated. Thank you.

You haven't created a struct containing a vector of structs. All you've done is created a struct which contains a struct. You do have a vector of structs but that is in main, not in any struct.

You also have miscellaneous other issues, global variables you aren't using and unnecessary struct uses.

Here's how you should do it

struct Vehicle {
    string LicensePlate;
    string VehicleType;
    vector<string> DamageType;
    int EstFixDays;
};

struct DailyCustomers {
    string date;
    vector<Vehicle> vehicleList; // a vector of structs inside a struct
};

int main() {
    DailyCustomers dlc;
    dlc.date = fileVector[1];
    for (int i = 1; i < fileVector.size(); i++) {
        stringDevide(fileVector[i]);
        Vehicle veh;
        veh.LicensePlate = licensePlate;
        veh.VehicleType = vehicleType;
        veh.DamageType = vehicleDamageType;  
        veh.EstFixDays = est; 
        dlc.vehicleList.push_back(veh);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM