std::array 的 std::array 是否具有连续内存? [英] Does std::array of std::array have contiguous memory?
问题描述
似乎,我找到了如何在 2 行代码中轻松获得具有连续内存的普通 2D 数组:
Seems, that I found how to easily get normal 2D Array with contiguous memory in 2 lines of code:
template<int N, int M>
using Array2D = array<array<int, M>, N>;
让我们解决在Array2D
(一点点c++17)中交换最小值和最大值的简单任务:
Let's solve easy task of swapping min and max in Array2D
(a little of c++17):
template<int N, int M>
void printArray2D(const Array2D<N, M> &arr);
int main() {
const int N = 5;
const int M = 5;
Array2D<N, M> arr;
// random init of Array2D
generate(arr.front().begin(), arr.back().end(), []()->int {
return rand() % 100;
});
printArray2D(arr);
auto[a, b] = minmax_element(arr.front().begin(), arr.back().end());
cout << "Swap minimum and maximum: " << *a << " " << *b << endl << endl;
iter_swap(a, b);
printArray2D(arr);
return 0;
}
template<int N, int M>
void printArray2D(const Array2D<N, M> &arr) {
for (const auto &row : arr) {
for (const auto &elem : row) {
cout << std::setw(3) << elem;
}
cout << endl;
cout << endl;
}
}
我在 Visual Studio 2017 中得到了下一个结果:
I got next result in Visual Studio 2017:
41 67 34 0 69
24 78 58 62 64
5 45 81 27 61
91 95 42 27 36
91 4 2 53 92
Swap minimum and maximum: 0 95
41 67 34 95 69
24 78 58 62 64
5 45 81 27 61
91 0 42 27 36
91 4 2 53 92
优点:
- 只需 2 条简单的线即可获得二维数组
- 您通常可以通过
arr[2][2]
访问元素 - 您可以使用 stl 算法
缺点:
- 这个解决方案在调试模式下不能正常工作,我有运行时错误
array iterators incompatible
- 我不知道内存是否总是连续分配
- 我不知道它是否适用于其他编译器
- 魔法迭代器
问题:
Array2D
的连续分配有什么保证吗?- 以这种方式使用数组迭代器是否符合条件?(不同的迭代器,但要记住指针的连续性和实现)
- 在生产代码中以这种方式(如示例)使用
Array2D
是否安全?如果没有,您能否以最少的代码开销提供解决此任务的良好代码? - geza:这个answer 与嵌套数组的连续性相矛盾.也许 C++14 中发生了一些变化?
- Is contiguous allocation for
Array2D
ensured by anything? - Is it eligible to use array iterators in this way? (different iterators, but bear in mind contiguous and implementation on pointers)
- Is
Array2D
safe to use in this manner (as in example) in production code? If not, can you present good code for solving this task with minimum code overhead? - geza: This answer contradicts to continuity of nested arrays. Maybe something has changed in C++14?
推荐答案
根据标准,内存应该是连续的.26.3.7.1 [array.overview] 段落说明(强调我的):
According to the standard the memory should be contiguous. The 26.3.7.1 [array.overview] paragraph states (emphasis mine):
头部定义了一个用于存储固定大小的类模板对象序列.数组是一个连续的容器.一个实例of array 存储 N 个类型为 T 的元素,因此 size() == N 是一个不变的.
The header defines a class template for storing fixed-size sequences of objects. An array is a contiguous container. An instance of array stores N elements of type T, so that size() == N is an invariant.
更新:似乎实现可能包括填充.这些 SO 帖子中有关该主题的更多信息:
std::array的大小是标准定义的吗?
特别是这个答案:
嵌套 std::array 中的数据是否保证连续?
Update: It appears the implementation might include the padding.
More info on the subject in these SO posts:
Is the size of std::array defined by standard?
and specifically this answer:
Is the data in nested std::arrays guaranteed to be contiguous?
这篇关于std::array 的 std::array 是否具有连续内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!