How to modify a nested struct on a vector?

后端 未结 3 778
粉色の甜心
粉色の甜心 2021-01-29 10:20

I am working on a program that keeps inventory of vehicles, so I created a struct for it. It also needs to keep a list of the drivers, so I created a nested struct for that. Her

相关标签:
3条回答
  • 2021-01-29 10:39

    Example of your own code:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <limits>
    
    struct Vehicle {
        std::string License;
        std::string Place;
        int Capacity;
        struct Driver {
            std::string Name;
            int Code;
            int Id;
        }dude;
    };
    
    void AddVehicle(std::vector<Vehicle> &vtnewV)
    {
        Vehicle newV;
        std::cout << "Enter license plate number: " << std::endl;
        std::cin >> newV.License;
        std::cout << "Enter the vehicle's ubication: " << std::endl;
        std::cin >> newV.Place;
        std::cout << "Enter the vehicle's capacity: " << std::endl;
        std::cin >> newV.Capacity;
        std::cout << "Enter the driver's name: " << std::endl;
        std::cin >> newV.dude.Name;
        std::cout << "Enter the driver's code: " << std::endl;
        std::cin >> newV.dude.Code;
        std::cout << "Enter the driver's identification number: " << std::endl;
        std::cin >> newV.dude.Id;
        vtnewV.push_back(newV);
    };
    
    int main()
    {
        std::vector<Vehicle> listVehicle;
        AddVehicle(listVehicle);
        AddVehicle(listVehicle);
    
        for (auto& i : listVehicle)
        {
            std::cout << i.dude.Name << " got crabs" << std::endl;
        }
    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return 0;
    }
    

    Now, I need to know if there's a way to add the driver on another function, like, you ask for the vehicle info on one function and then ask for the driver's info on another.

    I don't know if this is what you are after but there is a way better way to solve this than doing it this way, but without changing too much of your code, this will give you a hint:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <limits>
    
    struct Vehicle {
        std::string License;
        std::string Place;
        int Capacity;
        struct Driver {
            std::string Name;
            int Code;
            int Id;
        }driver;
    };
    
    Vehicle CreateVehicle()
    {
        Vehicle vehicle;
        std::cout << "Enter license plate number: " << std::endl;
        std::cin >> vehicle.License;
        std::cout << "Enter the vehicle's ubication: " << std::endl;
        std::cin >> vehicle.Place;
        std::cout << "Enter the vehicle's capacity: " << std::endl;
        std::cin >> vehicle.Capacity;
    
        return vehicle;
    };
    
    Vehicle::Driver CreateDriver()
    {
        Vehicle::Driver driver;
        std::cout << "Enter the driver's name: " << std::endl;
        std::cin >> driver.Name;
        std::cout << "Enter the driver's code: " << std::endl;
        std::cin >> driver.Code;
        std::cout << "Enter the driver's identification number: " << std::endl;
        std::cin >> driver.Id;
    
        return driver;
    }
    
    int main()
    {
        std::vector<Vehicle> listVehicle;
    
        auto vehicle = CreateVehicle();
        auto driver = CreateDriver();
        vehicle.driver = driver;
    
        listVehicle.push_back(vehicle);
    
    
        for (auto& i : listVehicle)
        {
            std::cout << i.driver.Name << " got crabs" << std::endl;
        }
    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-29 10:50

    How to modify a nested struct on a vector?

    To set the driver of the N-th item from the vector of Vehicles, you'd use:

    Vehicle::Driver driver_dude;
    ...
    ...
    vtnewV[N-1].dude = driver_dude;
    
    0 讨论(0)
  • 2021-01-29 10:51
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
     struct Driver
     {
          string Name;
          int Code;
          int Id;
    };
    
    struct Vehicle
    {
       string License;
       string Place;
       int Capacity;
       ///from my opinion driver should be declare like this
       Driver driver;
    
    };
    
    vector <Vehicle> vtnewV ;
    /// the vector need to declare outside the void
    /// else it will keep recreate a new vector
    void AddVehicle()
    {
       Vehicle newV;
    
       cout << "Enter license plate number: " << endl;
       cin >> newV.License;
       cout << "Enter the vehicle's ubication: " << endl;
       cin >> newV.Place;
       cout << "Enter the vehicle's capacity: " << endl;
       cin >> newV.Capacity;
       cout << "Enter the driver's name: " << endl;
       cin >> newV.driver.Name;
       cout << "Enter the driver's code: " << endl;
       cin >> newV.driver.Code;
       cout << "Enter the driver's identification number: " << endl;
       cin >> newV.driver.Id;
       vtnewV.push_back(newV);
    };
    void ShowInfo()
    {
        for(int i= 0; i<vtnewV.size();i++)
        {
               cout<<vtnewV[].License<<newV.driver.Id;
               ///you need to use for loop to cout all that exist in vector info
        }
    
    }
    int main()
    {
        AddVehicle();
        ShowInfo();
        return 0;
    }
    

    I have modify and added some comment to your code base by my own opinion hope that will solve you problem

    0 讨论(0)
提交回复
热议问题