是否可以在C ++中获取std :: array的子数组? [英] Is it possible to get a sub-array of a std::array in C++?
问题描述
我想做类似的事情
std::array<int, 5> array1 = {{ ... }};
const std::array<int, 3>& array2 = array1[1:4]; // [x:y] doesn't exist
也就是说,得到一个排序数组
That is, get an array that is a sort of view of another array, without having to copy it.
推荐答案
不,您不能那样做。所有标准库容器都是其数据的唯一所有者, std :: array
也不例外。实际上, std :: array
被限制为以某种方式实现,以便将元素存储在类的实际数组成员中,这将使别名变得不可能。
No, you can't do that. All standard library containers are unique owners of their data, and std::array
is no exception. In fact, std::array
is constrained to be implemented in such a way so that the elements are stored inside an actual array member of the class, which would make aliasing impossible.
有一个关于 array_view
类的建议,该类表示对连续数据块的非所有权视图。您可以在此处进行阅读。我不知道此提案的状态(当前的C ++标准化过程使我感到困惑)。
There is a proposal for an array_view
class that would represent a non-owning view into a contiguous block of data. You can read about it here. I don't know the status of this proposal (the current C++ standardization process confuses me).
这篇关于是否可以在C ++中获取std :: array的子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!