在初始化时预填充 std::vector ?
我想创建一个 double 向量的向量向量,并希望它已经有 (32,32,16) 个元素,而不需要手动将所有这些元素推回去。有没有办法在初始化期间做到这一点? (我不在乎被推动的价值是什么)。
我想要一个三维数组,第一维有 32 个元素,第二维有 32 个元素,第三维有 16 个元素。
I want to create a vector of vector of a vector of double and want it to already have (32,32,16) elements, without manually pushing all of these back. Is there a way to do it during initialization? (I don't care what value gets pushed).
I want a 3-dimensional array, the first dimension has 32, the second dimension has 32 and the third dimension has 16 elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一行:
或者打破界限:
我会说这分配了相当多的对象(32*32*16)。
单个向量有效吗?
这将减少 32*32*16-1
new
。One liner:
Or breaking the lines:
I would remark that this allocate a fair lot of objects (32*32*16).
Would a single vector work ?
That would be 32*32*16-1 less
new
.向量的向量之一允许您指定要复制到向量元素中的大小和值。我不太确定“(32,32,16)”元素是什么意思,但你可以这样做:
One of the ctors for a vector allows you to specify both the size and a value to be copied into the elements for the vector. I'm not quite sure what you means by "(32,32,16)" elements, but you could do something like:
给你:
我什至不会问你为什么想要这样一个怪物,而不是仅仅使用一个带有你自己的索引方案的
向量
。Here you go:
I am not even going to ask why you would want such a monstrosity and not just use a single
vector
with your own indexing scheme.由于大小是事先已知的,并且您需要将整个数组转换为一维数组(对于 OpenGL),为什么不为其创建一个类呢?我创建了一个基于模板的 3D 数组实现,希望对您有用。
用法:
Array3D 头文件:
您还可以使用以下函数获取指向 1D 数组的指针:
double* double_array = array.getArray();< /code>
编辑:更改了用法以用 double 而不是 int 显示。
Since the size is known before hand and you will need to convert the whole array to a 1D array (for OpenGL) why not create a class for it? I have created a template-based implementation of a 3D array which I hope will be useful to you.
Usage:
The Array3D header file:
You can also get the pointer to the array as a 1D by using the function:
double* double_array = array.getArray();
EDIT: Changed the usage to show it with double rather than int.