【发布时间】:2019-09-28 00:31:25
【问题描述】:
我想遍历一个结构的成员。换句话说,我较大的结构向量内部有一个较小的结构。在这种情况下,我想访问 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;
}
}
关于如何遍历一个结构存在问题,但如果它是多个结构,例如我的,我找不到任何解决方案。
【问题讨论】:
-
不是超级相关,但我建议查看基于范围的 for 循环。它们本质上是手动迭代器循环的一些不错的语法糖。
-
我知道我朋友的问题,但它不一样,你也可以看到,我的问题里面有多个结构
-
当然可以,可能类似于:
for( big_struct const& struc : big_struct_obj ) cout << struc.small_struct_obj.name;