是否可以浏览结构
Is it possible to browse a structure?
我需要每10毫秒填充一个结构,在这个结构中有230个变量,只有bool
和int
。我的想法是首先将所有变量逐个随机填充和初始化。但是它写起来很长,看起来很难看。而且这种结构经常变化。
所以我的第二个想法是浏览结构(也许使用循环?)并随机填充每个变量
经过一番研究,我什么也找不到。那么,有没有一种方法可以浏览我的结构并随机填充变量?
谢谢你的帮助。
编辑:这是我想做的:
我有一个头文件包含结构:
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了解更多详细信息。
我会选择使用std::map
来存储成员的值。
class MyStructure
{
std::map< unsigned int, int > integerValues;
std::map< unsigned int, bool > booleanValues;
public:
bool & exampleBoolean = booleanValues[ 0 ];
int & exampleInteger = integerValues[ 0 ];
void randomIntegers() {
for( auto & integer : integerValues )
integer.second = randomInteger(); // put your favorite random here
}
};
这样你只会改变数据实际存储的位置,而不是你访问结构的方式。
我建议使用enum
和字段名来索引地图。
一种简单的方法是对相同类型的字段进行分组,然后使用特定类型的数组创建联合。
ideone.com示例
#include <iostream>
using namespace std;
union X
{
struct
{
int i1, i2, i3;
bool b1, b2, b3;
};
struct
{
int is[3];
bool bs[3];
};
};
std::ostream& operator<<(std::ostream& os, const X& x)
{
os << "{ ";
for (int i = 0; i < 3; ++i)
os << x.is[i] << ' ' << x.bs[i] << ' ';
return os << '}';
}
int main()
{
for (int i = 0; i < 3; ++i)
{
X x;
for (int j = 0; j < 3; ++j)
{
x.is[j] = rand();
x.bs[j] = rand() % 2;
}
std::cout << x << 'n';
}
}
(当然,您应该做一些比硬编码数组维度更好的事情,无论它在哪里索引,并允许int
和bool
s的数量独立变化,但这都是微不足道的....)
{ 1804289383 0 1681692777 1 1957747793 1 }
{ 719885386 0 596516649 1 1025202362 1 }
{ 783368690 1 2044897763 0 1365180540 0 }
你不能像你想的那样遍历结构的字段。
如果这是一个测试,那么你不应该调整结构来适应测试。相反,只需用"硬"的方式——填写每个字段。这一点也很重要,因为不同的领域肯定有不同的"随机"方式。例如,_statutGroundFlight
可能不能是任何整数值;它可能有一组有效值要测试
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 是否可以浏览结构