使用循环填充结构
Fill structure using loop
我有一个C++结构
struct Line {
int date;
int time;
float open;
float high;
float low;
float close;
float sd;
float long_mo;
float short_mo;
};
8 个字段。我想使用循环填充它。
int fields_count=1;
while (fields_count<=8) {
// get digit from outer sourse. I dont need help here.
// First iteration puts to 1 field, Second iteration puts to 2 field and so on up to last field of struct
fields_count++;
}
像这样:
#include<stddef.h>
struct Line {
int date;
int time;
float open;
float high;
float low;
float close;
float sd;
float long_mo;
float short_mo;
};
char types [] = "iifffffff";
int offsets [] = {
offsetof (Line, date),
offsetof (Line, time),
offsetof (Line, open),
offsetof (Line, high),
offsetof (Line, low),
offsetof (Line, close),
offsetof (Line, sd),
offsetof (Line, long_mo),
offsetof (Line, short_mo)
}
Line line;
for (int i = 0; i < 9; i++) {
char *field_ptr = ((char*)&line) + offsets [i];
if (types [i] == 'i')
*(int*)field_ptr = readInt ();
else if (types [i] == 'f')
*(float*)field_ptr = readFloat ();
}
在 C++20 中,您可以使用扩展语句来迭代聚合:
auto my_line = Line{};
auto fields_count = std::size_t{0};
for...(auto& member : my_line) {
member = get_digit(fields_count++);
}
您甚至可以获取正在迭代的成员的类型:
auto my_line = Line{};
auto fields_count = std::size_t{0};
for...(auto& member : my_line) {
using type = std::remove_cvref_t<decltype(member)>;
member = get_digit<type>(fields_count++);
}
可悲的是,C++中没有反射,所以没有一个好方法可以做你想做的事。
使用现代C++您可以执行以下操作:
#include <cstddef>
#include <iostream>
#include <tuple>
#include <utility>
struct Line
{
int date;
int time;
float open;
float high;
float low;
float close;
float sd;
float long_mo;
float short_mo;
};
template <typename ...P, std::size_t ...I, typename F>
void for_each_tuple_elem_low(std::tuple<P...> t, std::index_sequence<I...>, F &&func)
{
(void(func(std::get<I>(t))) , ...);
}
template <typename ...P, typename F> void for_each_tuple_elem(std::tuple<P...> t, F &&func)
{
for_each_tuple_elem_low(t, std::make_index_sequence<sizeof...(P)>{}, func);
}
int main()
{
Line obj;
auto &[x1,x2,x3,x4,x5,x6,x7,x8,x9] = obj;
auto tuple = std::tie(x1,x2,x3,x4,x5,x6,x7,x8,x9);
int i = 0;
for_each_tuple_elem(tuple, [&](auto &ref)
{
ref = i++;
});
}
在这里,样板简化为键入结构化绑定的名称两次:x1,x2,x3,x4,x5,x6,x7,x8,x9
。
相关文章:
- 使用结构成员指针在C++中填充结构
- C++:使用缓冲区中的数据填充结构
- 在AAPCS(ARM ABI)下,C/C++结构填充的稳定性如何
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 用二进制文件填充结构
- 哪些值存储在对齐的结构/类对象的填充字节中
- 使用初始化列表填充C++中的多维结构数组时出现问题
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 与STD :: BITSET的工会成员的结构填充
- 是否有任何环境会导致"int"结构填充?
- 如何在编译时计算c++11中的结构填充
- C++:OpenGL,glm和结构填充
- 关于结构填充
- 赛灵思微火焰结构填充/填料
- C++结构填充和字段打包
- C++ 用结构填充 2D 矢量
- VS 2008/2010中是否控制尾部结构填充?(#pragma包不够好)
- 64位计算机上的结构填充
- Visual Studio 2008 - 关于 C++ 中的结构填充
- 数据结构填充