作为参考,我目前在连续内存块中创建二维数组的方式是首先创建一个长度为 N 的 float* 数组(动态),将所有 N*5 个浮点数分配到一个数组中,然后复制每个数组的地址第 5 个元素进入 float* 的第一个数组.
For reference the way I currently create a 2D array in a contiguous memory block is by first making a (dynamic) array of float* of length N, allocating all N*5 floats in one array and then copying the address of every 5th element into the first array of float*.
那不是二维数组,而是指针数组.如果你想要一个真正的二维数组,这就是它的完成方式:
float (*p)[5] = new float[N][5];
p [0] [0] = 42; // access first element
p[N-1][4] = 42; // access last element
delete[] p;
注意只有一个分配.我可以建议阅读更多关于在 C++ 中使用数组的内容吗?
这篇关于std::vector 和多维数组的连续内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!
本文链接:https://www.f2er.com/3186444.html