我可以编写代码如
typedef boost::multi_array<boost::int32_t,3> data_t; // 3d -- typedef data_t::array_view<3>::type data_3d_view_t; // 2d -- typedef data_3d_view_t::reference data_2d_subarray_t; typedef data_t::array_view<2>::type data_2d_view_t;
然后我可以通过类型data_2d_subarray_t或data_2d_view_t访问一个2d切片.
他们有什么区别?
我能做什么与我不能做的另一个?
有没有性能差异?
非常感谢您对我的澄清.
最好的祝福,
rodrigob.
解决方法
reference This is the reference type of the contained value. If NumDims == 1,then this is element&. Otherwise,this is the same type as template subarray::type.
template array_view::type This is the view type with Dims dimensions. It is returned by calling operator. It models MultiArray.
所以他们是不同的类型,首先.在这种情况下,视图表示MultiArray的一种子集合.他们实现了MultiArray概念,但它们包含的元素实际上是其他MultiArray的元素.视图允许您将新的index定义为MultiArray的元素.例如,您可以定义一个视图来反转索引,以便视图的第一个元素是MultiArray的最后一个元素.从documentation:
A view lets you treat a subset of the underlying elements in a MultiArray as though it were a separate MultiArray. Since a view refers to the same underlying elements,changes made to a view’s elements will be reflected in the original MultiArray.
MultiArrays是递归定义的;尺寸为n的多阵列1可以被认为是维数n-1的多阵列数组,它们是子数组.子阵列和视图之间的关键区别在于,您可以将多阵列分割成沿着任何轴(包括主轴)的较小维度的视图,但不能沿主轴切片子阵列.
data_t d3(boost::extents[4][5][6]); data_2d_view_t d2_view = d3[boost::indices[range(0,4,2)][1][range(0,6,3)]]; data_2d_subarray_t d2_sub = d3[1]; // the following,and anything like it,won't work data_2d_subarray_t d2_sub_b = d3[range(0,2)][0];
我不认为有什么重大的性能差异,尽管这取决于您在创建视图时使用的索引类型.观点可能略逊一筹,但不是大O观.