[英]How to assign / copy a Boost::multi_array
I want to assign a copy of a boost::multi_array. 我想分配一个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
? 用什么代替
m_f = f
?
You should resize m_f
before assigning. 您应该在分配前调整
m_f
大小。 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. 将
short
转换为char
将是隐式的。 You should consider using std::transform
if you want explicit conversion. 如果要显式转换,则应考虑使用
std::transform
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.