如何分配/复制一个boost :: multi_array的 [英] How to assign / copy a Boost::multi_array

查看:428
本文介绍了如何分配/复制一个boost :: multi_array的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分配一个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.

这code不起作用,因为尺寸和大小是不一样的。

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;
  }
}

,而不是 m_f =的使用什么˚F

推荐答案

您应该分配前调整 m_f 。它可能看起来像下面的示例中:

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;
}

可能有一个更好的办法。转换字符将是隐含的。您应该考虑使用的std ::变换如果你想显式转换。

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.

这篇关于如何分配/复制一个boost :: multi_array的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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