How to modify a nested struct on a vector?

你。 提交于 2019-12-03 00:27:23

问题


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. Here's the code:

struct Vehicle {
    string License;
    string Place;
    int Capacity;
    struct Driver {
        string Name;
        int Code;
        int Id;
    } dude;
};

I ask for user input and then put the structs in a vector using this function:

void AddVehicle(vector<Vehicle> &vtnewV) {
    Vehicle newV;
    Vehicle::Driver dude;
    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 >> dude.Name;
    cout << "Enter the driver's code: " << endl;
    cin >> dude.Code;
    cout << "Enter the driver's identification number: " << endl;
    cin >> dude.Id;
    vtnewV.push_back(newV);
};

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. The user enters the license plate, for example, and the driver is added in the struct that has that license plate. I don't know if I'm explaining myself. So, that's it, if you can help me, I would really appreciate it.


回答1:


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;
}



回答2:


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;



回答3:


#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



来源:https://stackoverflow.com/questions/32366226/how-to-modify-a-nested-struct-on-a-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!