C++更改结构在一个 vector 内的值,在一个结构内,在另一个 vector 内

标签 c++ c++11 vector struct

我有以下代码(在 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;
            }
        }
    }

}

出于某种原因,我从未进入I NEVER GET HERE!!! 代码...

是否有一种特殊的方法可以在 vector 内的结构内设置 vector 内的结构值?我在这里遗漏了什么吗?

我使用的是 C++11、gcc linux。

感谢您的帮助。

最佳答案

当你有这样的事情时:

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

这会遍历设备 vector 中的所有 DeviceStruct 值。但是,“自动设备”的意思是“给我一份该项目的拷贝”。

一旦你这样做了,你所做的任何修改都会对该拷贝进行 - 当你到达循环体的末尾时,它会被丢弃。

在你的情况下你想说:

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

此处的“&”表示“给我设备的引用”。现在“设备”指的是 vector 中的实际元素(而不是它的拷贝),因此您对其所做的任何更改都会对原始设备进行。

关于C++更改结构在一个 vector 内的值,在一个结构内,在另一个 vector 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426641/

相关文章:

c++ - 使用文件 - 只改变一个字符

c++ - Boost strand post 在访问/插入值时导致段错误

c++ - 如何将 vector<pair<int, int>> 传递给函数?

C++ vector 和错误 "no instance of overloaded function"

c++ - 帮助管理 iostream

c++ - 将自定义类型作为 map 的可能键的可能方法有哪些

C++0x 与 Qt Creator

c++11 - 是否可以将 std::array move 到 std::vector 中?

C++如何删除 vector 中的节点

c++ - 使用 CIMg 和 C++ 进行傅里叶滤波