[英]C++ extracting data from deque of struct
我有一個雙端結構的雙端隊列,我需要提取一個特定的屬性。
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;
};
例如我
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
...
如果我需要例如speed_ms屬性,我想返回一個數組,如:
0.00
0.00
0.00
...
但是提取的屬性在此功能之前尚不明確,這取決於需要。 我在想這樣的功能:
function extract (string propertie_to_extract = "speed_ms", deque<struct way_point> way_point){
retrun vector[i]=way_point[i]."propertie_to_extract"}
正如@Bo在評論中提到的
您不能在運行時形成變量名。
但是您可以為結構的每個成員實現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
然后模板化包裝器功能可以解決您的問題
template<typename T>
T getData(std::function<T(way_point&)> f, way_point& wp)
{
return f(wp);
}
並使用您需要的變量的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);
並在雙端隊列中的每個結構實例上調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.