简体   繁体   English

C ++更改向量内部,结构内部,另一个向量内部的结构的值

[英]C++ Change value of structs inside a vector, that is inside a struct, that is inside another vector

I have the following code (found at this link here ): 我有以下代码(可在此处找到此链接 ):

    // Example program
#include <iostream>
#include <vector>
#include <chrono>
#include <string>
#include <sstream>

struct ControlStruct {
    std::string port;
    bool timeoutOn;
    int detectionTimeout;
    bool state;
};

struct DeviceStruct {

    std::string name;

    std::vector<ControlStruct> deviceControls;

};

std::vector<DeviceStruct> devices;

int main()
{
    for (int i = 0; i < 10; i++)
    {
        ControlStruct cs;

        DeviceStruct device;

        std::stringstream ss;
        ss << "Device" << i;
        device.name = ss.str();

        for(int k = 0; k < 5; k++)
        {
            ControlStruct cs;

            ss.clear();
            ss << "Port" << i;
            cs.port = ss.str();

            cs.state = false;
            cs.timeoutOn = false;
            cs.detectionTimeout = (k * 2) + 1;

            device.deviceControls.push_back(cs);
        }

        devices.push_back(device);
    }


    //
    // Start devices
    //
    for (auto device : devices)
    {
        for (auto control : device.deviceControls)
        {
            control.timeoutOn = true;
            control.state = true;

            std::cout << "Starting device " << device.name << " with timeout of " << control.detectionTimeout << " sec." << std::endl;
        }

    }

    while (1)
    {
        for (auto device : devices)
        {
            for (auto control : device.deviceControls)
            {
                if (!control.state)
                    continue;

                std::cout << "I NEVER GET HERE!!!!!!!!!!!!" << std::endl;

                control.detectionTimeout--;

                if (control.detectionTimeout == 0)
                {
                    control.state = false;
                    std::cout << "Device " << device.name << " timed-out." << std::endl;
                }
                else
                    std::cout << "Device " << device.name << " count = " << control.detectionTimeout << std::endl;
            }
        }
    }

}

For some reason, I never get into the I NEVER GET HERE!!! 由于某些原因,我永远不会进入“ I NEVER GET HERE!!!不会进入I NEVER GET HERE!!! code... 码...

Is there a special way to set values of structs inside an vector inside a struct that is inside a vector ? 是否有一种特殊的方法来设置矢量内部结构中矢量内部结构的值? Am I missing something here ? 我在这里想念什么吗?

I´m using C++11, gcc linux. 我正在使用C ++ 11,gcc linux。

Thanks for helping. 感谢您的帮助。

When you have something like this: 当你有这样的事情:

std::vector<DeviceStruct> devices;
for (auto device : devices) { ... }

This iterates through all the DeviceStruct values in the devices vector. 这将遍历设备向量中的所有DeviceStruct值。 However, "auto device" means "give me a copy of the item". 但是,“自动设备”的意思是“给我该项目的副本”。

Once you do that, any modifications you make are made to that copy - which is then thrown away when you get to the end of your loop body. 完成此操作后,您将对该副本进行任何修改-到达循环正文的结尾时,该副本将被丢弃。

In your case you want to say: 您想说的是:

for (auto& device : devices) { ... }

The '&' here means "give me a reference to the device". 这里的“&”表示“给我对该设备的引用”。 Now 'device' refers to the actual element inside the vector (rather than a copy of it) so any changes you make to it are made to the original device. 现在,“设备”指的是向量中的实际元素(而不是其副本),因此您对其进行的任何更改都将对原始设备进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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