c++ - 遍历结构 C++ 中的结构

标签 c++ loops iteration

<分区>

我想遍历一个结构的成员。换句话说,我更大的结构 vector 内部有一个更小的结构。我想访问所有内部结构,在这种情况下是 small_strcut_subject:

#include <iostream>
#include <vector>

#include "../common/myheader.h"

using namespace std;

struct small_struct {
    string name;

};

struct big_struct {
    struct small_struct small_struct_obj;
};


int main() {


    std::vector<big_struct> big_struct_obj;

    big_struct_obj.push_back(big_struct());

    big_struct_obj[0].small_struct_obj.name = "english";


    for (std::vector<big_struct>::iterator it = big_struct_obj.begin(); it != big_struct_obj.end(); ++it){

//      cout << big_struct_obj[*it].small_struct_obj.name << endl;
    }
}

关于如何遍历一个 sttuct 的问题,但如果它是多个结构,比如我的,我找不到任何解决方案。

最佳答案

你可以这样做:

  cout << it->small_struct_obj.name << endl;

如果您知道如何遍历结构 vector ,剩下的就是访问 for 循环中的结构成员,这可以通过 -> 运算符。

关于c++ - 遍历结构 C++ 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071819/

相关文章:

c++ - 如何正确解决C++11中的生产者消费者

c++ - 将多维数组加载到 C++ 中的数据文件中

python - 用while循环python写阶乘

C++ STL : How to iterate vector while requiring access to element and its index?

c++ - 被间接乘法算法困扰

lisp - 在 (loop ...) 中使用反引号/逗号成语是否正确?

c++ - 调用另一个类c++中的成员函数

c++ - ICC (14.0) 甚至无法在 Ubuntu 13.10 中编译 hello-world?

python - while 循环中定义的变量无法定义

JAVA 具有定义的数字序列的循环