如何修改向量上的嵌套结构? [英] How to modify a nested struct on a vector?

查看:83
本文介绍了如何修改向量上的嵌套结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在,我需要知道是否有一种方法可以将驱动程序添加到另一个函数,请求一个功能的车辆信息,然后在另一个功能请求驾驶员的信息。例如,用户输入牌照,并且将驱动程序添加到具有该牌照的struct中。我不知道我是否在解释自己。所以,这是它,如果你能帮助我,我会真的很感激。

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.

推荐答案

您自己的代码示例:

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

这篇关于如何修改向量上的嵌套结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆