【问题标题】:How to access and store struct members of vector type?如何访问和存储向量类型的结构成员?
【发布时间】:2018-03-16 13:43:57
【问题描述】:

我在一个公共模块(类)中有一个函数,它采用向量类型的结构引用: 该结构有一个向量类型的元素。

请参考以下代码sn-p:

bool getFrameInfo(ABC& abc);

struct ABC {
    int numberOfFrames;
    std::vector<XYZ> framInfo;
    ....
    }

    struct XYZ {
    int size;
    Position Pos;
    ...
    }

我需要访问和存储 struct XYZ 的成员在我的类中的成员变量或局部变量中,它调用公共模块中定义的函数来获取框架信息。 请提出访问和存储“XYZ”结构成员的方法,以便我可以在我的程序中重复使用它们来绘制框架。

【问题讨论】:

    标签: c++ function vector struct stdvector


    【解决方案1】:

    例子:

    bool getFrameInfo(ABC& abc) {
        abc.framInfo[0].size = 10;
        // more code, where you return something somewhere
    }
    

    这将从abc 访问你的向量,然后索引它的第一个元素(我假设它存在),并访问你的向量第一个结构的size 字段。它在其size 中存储了 10 个。

    【讨论】:

    • 我的 struct XYZ 可以存储超过 1 帧的信息。我可以使用简单的 for 循环访问元素还是需要使用迭代器?
    • 你可以使用@user660760。
    • 你需要从函数中返回一些东西。
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2022-09-22
    • 2013-05-31
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-12-17
    • 2022-01-04
    相关资源
    最近更新 更多