[英](c++) Storing lambda function in a boost multi_array
我的計划是在boost庫的多維數組multi_array中存儲數百(甚至數千)個(插值)函數。 我需要存儲它們,因為我需要在項目的不同點調用它們,不同的數字作為參數。 (我使用linterp庫http://rncarpio.github.io/linterp/來創建插值函數)。
我可以將函數存儲在矢量中,如下所示:
// creating the vector, storing the function
std::vector< std::function< double(double *x) > > interp_list(4);
// storing the function in the vector
interp_list[0] = ( [&] (double *x) { return interp1.interp(x); } );
但是,對多維數組執行相同操作始終會導致編譯錯誤:
// creating the array, I want to store the functions in
boost::multi_array< std::function<double (std::vector<double>::iterator)>, 2> interp2_list[2][2];
// storing the function in the vector
interp2_list[0][0] = ( [&] (std::vector<double>::iterator x) { return interp1.interp(x); } );
我對函數至少有“7維”(例如interp_list [6] [2] [3] [3] [64] [12] [2]),因此喜歡循環它。
編輯1.0:添加錯誤消息:
在/usr/include/boost/multi_array.hpp:26:0中包含的文件中,來自./StoreInterp.cpp:16:/usr/include/boost/multi_array/multi_array_ref.hpp:在Instanziierung von»boost :: multi_array_ref&boost :: multi_array_ref :: op erator =(const ConstMultiArray&)[with ConstMultiArray = main():::: iterator)>; T = std :: function>)>; long unsigned int NumDims = 2ul]«:/usr/include/boost/multi_array.hpp:371:26:erfordert durch»boost :: multi_array&boost :: multi_array :: o perator =(const ConstMultiArray&)[with ConstMultiArray = main( )::::迭代)>; T = std :: function>)>; long unsigned int NumDims = 2ul; Allocator = std :: allocator>)>>]«./StoreInterp.cpp:108:22:von hier erfordert /usr/include/boost/multi_array/multi_array_ref.hpp:482:30:Fehler:»const struct main() :::: iterator)>«沒有名為的成員»num_dimensions«BOOST_ASSERT(other.num_dimensions()== this-> num_dimensions());
你的interp2_list
聲明是錯誤的,你聲明了一個2維數組的boost::multi_array<>
2維度,所以你得到一個4-D“事物”,其中第2維度為2,但最后一個維度沒有兩個維度。
你真正想要的是用正確的尺寸初始化的單個boost::multi_array<>
:
boost::multi_array< std::function<double (std::vector<double>::iterator)>, 2>
interp2_list(boost::extents[2][2]);
請參閱增強文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.