问题描述
我正在将一些代码从c迁移到c ++。我已经更改了一些 malloc
结构内存分配到新的
调用。
基本上,我移植代码之前是包含多组框架协调的malloc'ing数组,每个数组都有几十万个浮点数,所以总数组长度可能在数千万的坐标。
我应该使用什么样的结构/容器?
我需要什么样的保护来捕捉内存相关的错误?
编辑1
我已经重新调整/改写了这个问题,以更准确地反映我想要做的事情。
我认为某种2D列表结构可能会做到这一点...可能是std :: deque的std :: deque(s)?
p>答案是 std :: vector
。
实际上你不需要太多的内存(或者你有一些内存受限的平台,我假设你会在这种情况下告诉我们)。矢量是完美的为此目的。你不必自己管理记忆。
如果您想要一次管理其中的几个,您可以使用向量向量。
但是现在有些10 ^ 6s的浮点数绝对不是很大的。
更新:还有一件事如果你与 deque
一起去。请不要通过循环中的索引访问 deque
对象。实际上 deque
在插入两边很强,但不是通过索引访问对象。可能不是在中间插入物体,就像我看到的那样。
I'm migrating some code from c to c++.
I've changed some malloc
calls for structure memory allocation to new
calls.
Basically before the code I'm porting was malloc'ing arrays that contain multiple sets of frame coords, each a couple hundred thousand floats in length -- so the total array length could be in the tens of millions of coordinates.
What kind of structure/container should I use?
And what kind of protections do I need to catch memory-related errors?
Edit 1
I've retitled/rephrased the question to more accurately reflect what I'm trying to do.
I'm think some sort of 2D list-like structure might do the trick... possibly a std::deque of std::deque(s)?
Answer is std::vector
.
You don't need that much memory actually (or you have some memory constrained platform, I assume you would have told us in that case). Vector is perfectly fine for this purpose. And you don't have to manage the memory yourself.
You can use vectors of vectors if you want to manage several of them at once.
But some 10^6s floats is definitely not a big deal nowadays.
Update: One more thing if you go with deque
. Please don't access deque
objects by index in loops. Actually deque
is strong at inserting at both sides, but not at accessing objects by index. And probably not at inserting objects in the middle, as I have seen somehere.
这篇关于在C ++中如何最好地存储非常大的2D浮动列表?错误处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!