How to assign / copy a Boost::multi_array

早过忘川 提交于 2019-12-09 10:32:27

问题


I want to assign a copy of a boost::multi_array. How can I do this. The object where I want to assign it to has been initialized with the default constructors.

This code does not work, because the dimensions and size are not the same

class Field {
  boost::multi_array<char, 2> m_f;

  void set_f(boost::multi_array<short, 2> &f) {
    m_f = f;
  }
}

What to use instead of m_f = f ?


回答1:


You should resize m_f before assigning. It could look like in the following sample:

void set_f(boost::multi_array<short, 2> &f) {
    std::vector<size_t> ex;
    const size_t* shape = f.shape();
    ex.assign( shape, shape+f.num_dimensions() );
    m_f.resize( ex );
    m_f = f;
}

May be there is a better way. Conversion short to char will be implicit. You should consider using std::transform if you want explicit conversion.



来源:https://stackoverflow.com/questions/1237723/how-to-assign-copy-a-boostmulti-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!