简体   繁体   English

如何在另一个结构向量中初始化一个结构向量?

[英]How to initialize a vector of struct inside another vector of struct?

I try to initialize structures of vectors that contains vector:我尝试初始化包含向量的向量结构:

    struct productionCanvas
    {
        int canvasID;
        int indexXmlJob;
    };
    
    struct productionArea
    {
        int areaID;
        std::vector<productionCanvas> canvasList;
    };

The first level is correctly initialized but not the second one:第一级已正确初始化,但第二级未正确初始化:

    int areaIncr=0;
    int canvasIncr=0;
    std::vector<productionArea> production;
    
    int addArea()
    {
        productionArea area{};
    
        areaIncr++;
        area.areaID = areaIncr;
        area.canvasList = {};
        production.push_back(area);
        return areaIncr;
    }
    
    int addCanvas(int job)
    {
        productionCanvas canvas{};
    
        canvasIncr++;
        canvas.canvasID = canvasIncr;
        canvas.indexXmlJob = job;
        for (productionArea area : production)
        {
            if (area.areaID == areaIncr)
            {
                area.canvasList.push_back(canvas);  // this line is triggered
                break;
            }
        }
        // Check if correctly push_back
        for (productionArea area : production)
        {
            for (productionCanvas canvas : area.canvasList)
            {
                // This line is never triggered, why ?
            }
        }
        return canvasIncr;
    }
    
    addArea();  // area correctly inserted
    addCanvas(1);  // canvas not inserted inside area
    .

There is no return value of the push_back function, so I cannot know why it's not pushed back. push_back function没有返回值,所以我不知道为什么它没有被推回。 It seemed that it does not work like Qt-QVector.它似乎不像 Qt-QVector 那样工作。

How to initialize these struct?如何初始化这些结构?

You can zero-initialize any POD struct or array by simply adding {} :您可以通过简单地添加{}来对任何 POD 结构或数组进行零初始化:

productionArea area{};
// Or
productionArea area = {};
// Or
auto area = productionArea{};

Unless you are working in C/C++ environment, there is no need for typedef struct in C++.除非您在 C/C++ 环境中工作,否则 C++ 中不需要typedef struct The symbol namespaces are not separate.符号命名空间不是分开的。 This is equal to your current code:这等于您当前的代码:

struct productionCanvas_def
{
    int canvasID;
    int indexXmlJob;
} productionCanvas;

You can even initialize each member:你甚至可以初始化每个成员:

struct foo{
    int a{2};
    int a{4};
};

But then it is no longer POD.但后来它不再是 POD。

After the question has been edited问题被编辑后

The problem is here:问题在这里:

for (productionArea area : production) // <-- The `area` is a copy of 
                                       //     the one in the container.
        {
            if (area.areaID == areaIncr)
            {
                area.canvasList.push_back(canvas);  // this line is triggered
                break;
            }
        }

To modify elements in place, you have to use references for(productionArea &area: production)要就地修改元素,您必须使用参考for(productionArea &area: production)

it doesn't look like c++, but the below code wiil not run as your canvasList with empty in addArea它看起来不像 c++,但下面的代码不会作为你的 canvasList 运行,addArea 中为空

area.canvasList = {};

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

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