c++ - 是否可以浏览结构?

标签 c++ structure

我需要每 10 毫秒填充一个结构,在这个结构中有 230 个变量,只有 boolint。我的想法是首先将所有变量一个一个地随机填充和初始化。但是写起来很长而且看起来很丑陋。这种结构也经常变化。

所以我的第二个想法是浏览结构(也许使用循环?)并随机填充每个变量。

经过一些研究,我找不到任何东西。那么,有没有办法浏览我的结构并随机填充变量?

感谢您的帮助。

编辑:这是我尝试做的:

我有一个包含结构的头文件:

    typedef struct
        {
            /// Statut Ground-Flight
            int _statutGroundFlight;
            /// Statut Capteur de presence
            bool _statutCapteurPrensence;
            /// Statut Bouton OPEN in
            bool _statutBoutonOpenIn;
            /// Statut Bouton OPEN ext
            bool _statutBoutonOpenExt;
            /// Statut Bouton CLOSE in
            bool _statutBoutonCloseIn;
            /// Statut Bouton CLOSE ext
            bool _statutBoutonCloseExt;
...

这是我每 10 毫秒要执行的操作:

//Create a structure
struct myStruct;

//Browse the structure
for(int i; myStruct.size(); ++i){
   if(myStruct[i] is int){
      //Fill it randomly with int
   }
   if(mystruct[i] is bool){
      //Fill it randomly with bool
   }
}

最佳答案

我不认为有一种方法可以随意浏览结构和填充变量,但是可以避免使用宏编写代码。 您可以编写宏并让预处理器为您生成代码.. 例如:

创建一个 struct.def,您可以在其中定义必填字段

#ifndef INTEGER
#error "INTEGER not defined"
#endif
#ifndef BOOL
#error "BOOL not defined"
#endif  
#ifndef create_struct 
#error "create_struct not defined"
#endif  


create_struct(my_struct,
        INTEGER(i1)
        BOOL(b1,false)
        INTEGER(i2)
        INTEGER(i3)
        BOOL(b2,true)
        BOOL(b3,false)
        BOOL(b4,false)
        INTEGER(i4)
        //add or modify fields here
)       

#undef INTEGER
#undef BOOL
#undef create_struct

然后使用上面的文件在你的代码中编写宏

#include "stdio.h"
#include "string.h"
//create structure
#define INTEGER(var_name) int var_name;
#define BOOL(var_name,data) bool var_name;
#define create_struct(struct_id,data_type)\
        typedef struct struct_id##_tag{\
                data_type\
        }struct_id;
#include "struct.def"
//-------------------------------------------

//function to initialize default value
#define INTEGER(var_name) p->var_name=0;
#define BOOL(var_name,data) p->var_name=data;
#define create_struct(struct_id,data_type)\
        void initialize_##struct_id(struct_id* p)\
        {\
                data_type\
        }
#include "struct.def"
//-------------------------------------------------

//function to fill random value to structure        
#define INTEGER(var_name) p->var_name=rand();
#define BOOL(var_name,data) p->var_name=rand()%2;
#define create_struct(struct_id,data_type)\
        void fill_random_##struct_id(struct_id* p)\
        {\
                data_type\
        }
#include "struct.def"
//-----------------------------------------

现在,如果您运行预处理器,那么它将为您生成下面提到的代码....

typedef struct my_struct_tag{
        int i1;
        bool b1; 
        int i2; 
        int i3;
        bool b2; 
        bool b3; 
        bool b4; 
        int i4; 
}my_struct;
void initialize_my_struct(my_struct* p) { 
        p->i1=0; 
        p->b1=false; 
        p->i2=0;
        p->i3=0; 
        p->b2=true;
        p->b3=false; 
        p->b4=false;
        p->i4=0; 
}
void fill_random_my_struct(my_struct* p) {
        p->i1=rand(); 
        p->b1=rand()%2;
        p->i2=rand(); 
        p->i3=rand(); 
        p->b2=rand()%2;
        p->b3=rand()%2;
        p->b4=rand()%2; 
        p->i4=rand();
}

现在,如果你想改变你的结构,那么你只需要在 struct.def 文件中的一个地方进行改变

您可以查看链接 http://rajenpandit.blogspot.in/p/using-macro.html了解更多详情。

关于c++ - 是否可以浏览结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24360778/

相关文章:

c++ - 从管道读取输入时出现 scanf 问题

c++ - 让回调记住一些信息的好方法(C++)

c++ - 二叉树 C++ 基础

c# - 将嵌套的 for 循环转换为单个 LINQ 语句

C - 将 void* 转换为 thrd_create() 中的结构

c++ - 获取内置运算符&()的&的类型?

c++ - 如何将这样的位图复制到DC?

c++ - 无法在 C++ 中定义结构数组

java - 从 Java 传递到 C 后未初始化的 JNA 结构数组

matlab - 从matlab中的结构数组中删除项目