[英]C++ extracting data from deque of struct
I got a deque of way-point struct, and i need to extract an specific properties. 我有一个双端结构的双端队列,我需要提取一个特定的属性。
struct way_point
{
double time_stamp_s;
double lat_deg;
double long_deg;
double height_m;
double roll_deg;
double pitch_deg;
double yaw_deg;
double speed_ms;
double pdop;
unsigned int gps_nb;
unsigned int glonass_nb;
unsigned int beidou_nb;
};
for example i got 例如我
28729.257 48.66081132 15.63964745 322.423 1.1574 4.8230 35.3177 0.00 0.00 0 0 0
28731.257 48.66081132 15.63964744 322.423 1.1558 4.8238 35.3201 0.00 1.15 9 6 0
28733.257 48.66081132 15.63964745 322.423 1.1593 4.8233 35.3221 0.00 1.15 9 6 0
...
and if I need for example the speed_ms properties, i would like to get back an array like : 如果我需要例如speed_ms属性,我想返回一个数组,如:
0.00
0.00
0.00
...
but the propreties to extract isn't known before the fonction, it's depending for the need. 但是提取的属性在此功能之前尚不明确,这取决于需要。 I was thinking of a function like this : 我在想这样的功能:
function extract (string propertie_to_extract = "speed_ms", deque<struct way_point> way_point){
retrun vector[i]=way_point[i]."propertie_to_extract"}
As @Bo mentioned in comments 正如@Bo在评论中提到的
you cannot form variable names at runtime. 您不能在运行时形成变量名。
But you may implement get-functions for every member of structure 但是您可以为结构的每个成员实现get-functions
double Get_time_stamp_s(way_point& wp) { return wp.time_stamp_s; }
double Get_gps_nb (way_point& wp) { return wp.gps_nb; }
// Rest of get-functions
Then templated wrapper function can solve your problem 然后模板化包装器功能可以解决您的问题
template<typename T>
T getData(std::function<T(way_point&)> f, way_point& wp)
{
return f(wp);
}
And call this wrapper with get-function of variable you need 并使用您需要的变量的get函数调用此包装器
way_point wp { 1.0, 2 };
double time_stamp_s_value = getData<double>(Get_time_stamp_s, wp);
unsigned int gps_nb_value = getData<unsigned int>(Get_gps_nb, wp);
And call it on every structure instance in deque. 并在双端队列中的每个结构实例上调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.