如何重新分配 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/