c++ - multi_array_view 赋值没有深拷贝?

标签 c++ boost boost-multi-array

如何重新分配 boost multi_array_view 以指向 multi_array 的不同部分?我不需要深层复制。

boost::multi_array<int, 2> a(...);
array_view b = a[indices[index_range(0, 5)][index_range()]];
array_view c = a[indices[index_range(0, 10)][index_range()]];
b = c; // don't work

boost 源:

template <typename ConstMultiArray>
multi_array_ref& operator=(const ConstMultiArray& other) {
    function_requires< 
      multi_array_concepts::
      ConstMultiArrayConcept<ConstMultiArray,NumDims> >();

    // make sure the dimensions agree
    BOOST_ASSERT(other.num_dimensions() == this->num_dimensions());
    BOOST_ASSERT(std::equal(other.shape(),other.shape()+this->num_dimensions(),
                            this->shape()));
    // iterator-based copy
    std::copy(other.begin(),other.end(),this->begin());
    return *this;
}

更新:我最终更改了程序中的方法,以保留对 boost::indices[...] 创建的索引对象的引用。然后我可以随时使用该对象创建一个新的 array_view

最佳答案

看起来像multi_array_ref紧密模拟 C++ 引用:

  • 不可重新安装
  • 从语义上讲,它是它“绑定(bind)到”的对象的别名

但有些令人惊讶的是,const_multi_array_ref 的情况似乎并非如此。 。请注意这些文档引用:

All of the non-const array types in this library provide assignment operatorsoperator=(). Each of the array types multi_array, multi_array_ref, subarray, and array_view can be assigned from any of the others, so long as their shapes match. The const variants, const_multi_array_ref, const_subarray, and const_array_view, can be the source of a copy to an array with matching shape. Assignment results in a deep (element by element) copy of the data contained within an array.

这讨论了分配给可变数组( View )。

但是:

Effects. This constructs a shallow copy of x.

方法

也许你可以使用const_multi_array_ref相反。

否则,您可能应该以与我们对 C++ 引用完全相同的方式“打破”不可重新安装引用的绑定(bind):std::reference_wrapper<>或类似的间接方式。

关于c++ - multi_array_view 赋值没有深拷贝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38316451/

相关文章:

c++ - 在类中定义公共(public)结构

c++ - 从先前的模板参数获取类型

c++ - 在 C++ 中使用来自类构造函数的参数初始化公共(public)属性

c++ - 提供维度后 boost::extent 对象的类型是什么?

c++ - pygraphviz 的 Python 3 pip 安装失败, "Microsoft Visual C++ is required",已安装 Visual Studio 2017

c++ - 从通过指向基类的指针存储的 boost 存档中读取对象如何工作?

c++ - boost::recursive_variant 接受 map,但拒绝 unordered_map

c++ - boost::dynamic_bitset 多线程问题

c++ - 如何将 Boost.MultiArray 的二维 View 作为函数的参数?

c++ - 如何在 Windows 上切换到另一个应用程序(使用 C++、Qt)?