C ++,boost,将对象存储在多维数组中而无需默认构造函数 [英] c++, boost, store objects in multidimensional array without default constructor

查看:64
本文介绍了C ++,boost,将对象存储在多维数组中而无需默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多维数组中存储数千个插值函数,最好是boost数组.主要问题是我使用的插值函数是一个没有默认构造函数的类.这禁止我初始化多维数组.

I want to store thousands of interpolation functions in a multidimensional array, preferable the one from boost. The main problem is that the interpolation function I use is a class that does not have a default constructor. This prohibits me to initialize the multidimensional array.

我希望我能做的事:

double func(const double& x1, const double& x2, const double& x3)
{
  return x1 + x2 + x3;
};

int main()
{
  std::vector<double> x1 {0, 1, 2, 3};
  std::vector<double> x2 {1.1, 1.2, 1.3, 1.4, 1.5};
  std::vector<double> x3 {0, 10, 20, 30, 40};
  std::vector<double> y(20, std::vector<double>(5));

  boost::multi_array<Linear_interp, 2> Storage(boost::extents[4][5]);

  typedef std::vector<double>::size_type vd_sz;
  int n = 0;
  for (vd_sz i_x1 = 0; i_x1 < x1.size(); ++i_x1) {
    for (vd_sz i_x2 = 0; i_x2 < x2.size(); ++i_x2) {
      for( vd_sz i_x3 = 0; i_x3 < x3.size(); ++i_x3) {
        y[n][i_x3] = func(x1[i_x1], x2[i_x2], x3[i_x3]);
      }
      Linear_interp myInterp(x3, y);
      Storage[i_x1][i_x2] = myInterp;
      ++n;
    }
  }

  // Sample usage
  double z = Storage[3][2].interp(23);

  return 0;
}

问题在于,Linear_interp类没有默认构造函数(该类类似于此类 1 ),因此boost :: multi_array无法初始化数组.

The problem is that the class Linear_interp has no default constructor (the class is similar to this class 1), therefore boost::multi_array can not initialize the array.

请注意,我在循环内初始化了所有插值,因此,我需要存储这些对象.一个简单的指向该对象的指针将不起作用,因为该对象将在每个循环中被覆盖.

Note that I initialize all interpolations inside a loop and therefore, I need to store these objects. A simple pointer to the object will not work, since the object will be overwritten in each loop.

实际上,我将拥有更多的尺寸(atm我有10个尺寸),并且multi_array是处理这些尺寸的不错的容器.此外,以后循环中的插值将采用先前循环中的插值(即我有一个递归问题).

In reality, I will have much more dimensions (atm I have 10), and multi_array is a nice container to handle these. Additionally, Interpolations in later loops, will take interpolations from previous loops (i.e. I have a recursive problem).

次代码更正.

代码更正:在以前的版本中,我没有保存导致不必要结果的"y".

EDIT 2: code correction: in the previous version, i did not save the "y"s which lead to unwanted results.

推荐答案

很好的指针将起作用.如果您将数组声明为:

Well pointer WILL work. If you will declare your array as:

multi_array<Linear_interp*, 2> 

存储指向对象而不是对象本身的指针.然后,在循环中,您可以在每次需要时分配新对象,并将其放置在数组中的适当位置.只需使用new关键字在循环内创建新的Linear_interp对象.这是在循环内使用的代码:

to store pointers to the the objects instead of objects themselves. Then in the loop you could allocate new object each time it would be necessary and put it to the appropriate place in the array. Just use new keyword to create new Linear_interp object inside the loop. Here is code to use inside the loop:

Storage[i_x1][i_x2] = new Linear_interp(x3, y);

这篇关于C ++,boost,将对象存储在多维数组中而无需默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆