(C )将lambda函数存储在Boost Multi_Array中

(c++) Storing lambda function in a boost multi_array

本文关键字:Multi Boost Array 存储 lambda 函数      更新时间:2023-10-16

我的计划是将数百个(甚至数千)(甚至数千个)函数存储在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); }  );

i至少具有函数的" 7个维度"(例如Interp_list [6] [2] [3] [3] [3] [64] [12] [2]),因此喜欢循环循环。

编辑1.0:添加错误消息:

在/usr/include/boost/multi_array.hpp:hpp:26:0中包含的文件中 来自./storeinterp.cpp:16:/USR/include/boost/multi_array/multi_array_ref.hpp:在Instanziierung von»boost»boost :: multi_array_ref&amp;BOOST :: MULTI_ARRAY_REF :: OPerator =(constmultiarray&amp;)[带有constmultiarray = main()::::: iterator)>;t = std :: function>)>;长期未签名的int numdims = 2ul]«:/USR/include/boost/multi_array.hpp:371:26:erfordert durch»boost :: multi_array&amp;boost :: multi_array :: operator =(constmultiarray&amp;)[带有constmultiarray = main()::::: iterator)>;t = std :: function>)>;长无符号int numdims = 2ul;分配器= std ::分配器>)>>>]«./storeinterp.cpp:108:22:von Hier Erfordert/USR/include/boost/multi_array/multi_array_ref.hpp: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-D数组但是最后两个维度没有任何程度。

您实际想要的是一个用正确尺寸初始初始化的boost::multi_array<>

boost::multi_array< std::function<double (std::vector<double>::iterator)>, 2> 
    interp2_list(boost::extents[2][2]);

请参阅Boost文档。