如何修改向量上的嵌套结构

How to modify a nested struct on a vector?

本文关键字:嵌套 结构 向量 何修改 修改      更新时间:2023-10-16

我正在开发一个保存车辆库存的程序,所以我为它创建了一个结构。它还需要保存驱动程序的列表,所以我为此创建了嵌套结构。这是代码:

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

我请求用户输入,然后使用以下函数将结构放入向量中:

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

现在,我需要知道是否有办法在另一个功能上添加驾驶员,比如,你在一个功能中询问车辆信息,然后在另一功能中询问驾驶员信息。例如,用户输入车牌,然后将驾驶员添加到具有该车牌的结构中。我不知道我是否在解释自己。就这样,如果你能帮我的话,我真的很感激。

您自己的代码示例:

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

现在,我需要知道是否有办法将驱动程序添加到另一个功能,比如,你在一个功能上询问车辆信息,然后询问另一个司机的信息。

我不知道这是否是你想要的,但有一种比这样做更好的方法来解决这个问题,但在不改变太多代码的情况下,这会给你一个提示:

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

如何修改向量上的嵌套结构?

要从车辆矢量中设置第N个项目的驾驶员,您可以使用:

Vehicle::Driver driver_dude;
...
...
vtnewV[N-1].dude = driver_dude;
#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;
}

我根据自己的意见修改并添加了一些注释到您的代码库中希望这将解决您的问题