【问题标题】:(c++) Storing lambda function in a boost multi_array(c++) 在 boost multi_array 中存储 lambda 函数
【发布时间】:2016-06-29 12:47:18
【问题描述】:

我的计划是在 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()::::iterator)>; T = std::function >)>; long unsigned int NumDims = 2ul;分配器 = 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());

【问题讨论】:

  • 编译错误通常伴随着错误信息。你读过那封邮件吗?它说什么?

标签: c++ function boost multidimensional-array lambda


【解决方案1】:

您对interp2_list 的声明是错误的,您正在声明一个二维数组boost::multi_array&lt;&gt;,因此您得到了一个二维“事物”,其第一个维度的范围为 2,但没有范围最后两个维度。

你真正想要的是一个用正确尺寸初始化的boost::multi_array&lt;&gt;

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

boost documentation

【讨论】:

  • 非常感谢。实际上我应该知道这一点,因为我在代码中的不同部分都这样做了,而且我实际上查看了文档!
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2012-02-29
相关资源
最近更新 更多