Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign / copy a Boost::multi_array

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 ?

like image 704
Peter Smit Avatar asked Aug 06 '09 08:08

Peter Smit


1 Answers

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.

like image 110
Kirill V. Lyadvinsky Avatar answered Sep 21 '22 13:09

Kirill V. Lyadvinsky