更改 C++ 的保留内存 向量

发布于 2024-07-09 13:48:12 字数 275 浏览 2 评论 0原文

我有一个有 1000 个“节点”的向量,

 if(count + 1 > m_listItems.capacity())
     m_listItems.reserve(count + 100);

问题是当我要重新填充它时,我也将其清除。

m_listItems.clear();

容量不变。 我使用了调整大小(1); 但这似乎并没有改变容量。 那么如何更改储备金呢?

I have a vector with 1000 "nodes"

 if(count + 1 > m_listItems.capacity())
     m_listItems.reserve(count + 100);

The problem is I also clear it out when I'm about to refill it.

m_listItems.clear();

The capacity doesn't change.
I've used the resize(1); but that doesn't seem to alter the capacity.
So how does one change the reserve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

世界和平 2024-07-16 13:48:12
vector<Item>(m_listItems).swap(m_listItems);

将再次缩小 m_listItemshttp://www.gotw.ca/gotw /054.htm (Herb Sutter)

如果你想清除它,就用一个空向量交换:

vector<Item>().swap(m_listItems);

这当然更有效。 (请注意,交换向量基本上意味着只是交换两个指针。没有什么真正耗时的事情发生)

vector<Item>(m_listItems).swap(m_listItems);

will shrink m_listItems again: http://www.gotw.ca/gotw/054.htm (Herb Sutter)

If you want to clear it anyway, swap with an empty vector:

vector<Item>().swap(m_listItems);

which of course is way more efficient. (Note that swapping vectors basicially means just swapping two pointers. Nothing really time consuming going on)

橘和柠 2024-07-16 13:48:12

您可以按照其他人的建议以及 http://www.gotw 中的说明交换向量。 ca/gotw/054.htm 但请注意,它不是免费的,您正在执行每个元素的副本,因为向量必须分配一个新的、更小的块内存,并将所有旧内容复制过来。 (交换操作本质上是免费的,但是您要与用原始向量数据的副本初始化的临时变量进行交换,这是不是免费的)

如果您事先知道向量有多大,那么您可以首先应该分配正确的大小,因此无需调整大小:

std::vector<foo> v(1000); // Create a vector with capacity for 1000 elements

如果您事先不知道容量,为什么浪费一点空间有关系呢? 是否值得花时间将每个元素复制到一个新的更小的向量(这就是 std::vector(v).swap(v) 所做的),只是为了节省几千字节的内存?

同样,当您清除向量时,如果您打算重新填充它,无论如何,将其容量设置为零似乎是一种令人印象深刻的时间浪费。

编辑

baash05:如果你有 1000000 个项目怎么办
10兆公羊。 你可以说
减少开销的量是
重要吗?

不可以。暂时调整矢量大小需要更多内存,因此如果您的内存有限,可能会破坏您的应用程序。 (在交换它们之前,您必须将原始向量和临时向量存储在内存中,因此此时您最终会使用两倍的 RAM)。 之后,您可能会节省少量内存(最多几 MB),但这并不重要,因为向量中的多余容量永远不会被访问,因此它将被推送到页面文件,因此不会首先计入您的 RAM 限制。

如果您有 1000000 个项目,那么您应该首先将向量初始化为正确的大小。

如果您做不到这一点,那么您通常最好不要理会该容量。 特别是自从您声明要重新填充向量以来,您绝对应该重用已经分配的容量,而不是不断地分配、重新分配、复制和释放所有内容。

你有两种可能的情况。 您要么知道需要存储多少元素,要么不知道。 如果你知道,那么你可以首先创建具有正确大小的向量,因此你永远不需要调整它的大小,或者你不知道,那么你还不如保留多余的容量,所以至少它当您重新填充矢量时,不必向上调整大小。

You can swap the vector as others have suggested, and as described in http://www.gotw.ca/gotw/054.htm but be aware that it is not free, you're performing a copy of every element, because the vector has to allocate a new, smaller, chunk of memory, and copy all the old contents over. (The swap operation is essentially free, but you're swapping with a temporary initialized with a copy of the original vector's data, which is not free)

If you know in advance how big the vector is, you should allocate the right size to begin with, so no resizing is necessary:

std::vector<foo> v(1000); // Create a vector with capacity for 1000 elements

And if you don't know the capacity in advance, why does it matter whether it wastes a bit of space? Is it worth the time spent copying every element to a new and smaller vector (which is what std::vector(v).swap(v) will do), just to save a few kilobytes of memory?

Similarly, when you clear the vector, if you intend to refill it anyway, setting its capacity to zero seems to be an impressive waste of time.

Edit:

baash05: what if you had 1000000 items
an 10 megs of ram. would you say
reducing the amount of overhead is
important?

No. Resizing the vector requires more memory, temporarily, so if you're memory-limited, that might break your app. (You have to have the original vector in memory, and the temporary, before you can swap them, so you end up using up to twice as much RAM at that point). Afterwards, you might save a small amount of memory (up to a couple of MB), but this doesn't matter, because the excess capacity in the vector would never be accessed, so it would get pushed to the pagefile, and so not count towards your RAM limit in the first place.

If you have 1000000 items, then you should initialize the vector to the correct size in the first place.

And if you can't do that, then you'll typically be better off leaving the capacity alone. Especially since you stated that you're going to refill the vector, you should definitely reuse the capacity that has already been allocated, rather than allocating, reallocating, copying and freeing everything constantly.

You have two possible cases. Either you know how many elements you need to store, or you don't. If you know, then you can create the vector with the correct size in the first place, and so you never need to resize it, or you don't know, and then you might as well keep the excess capacity, so at least it won't have to resize upwards when you refill your vector.

千紇 2024-07-16 13:48:12

您可以从此处尝试此技术

std::vector< int > v;
// ... fill v with stuff...
std::vector< int >().swap( v );

You could try this technique from here

std::vector< int > v;
// ... fill v with stuff...
std::vector< int >().swap( v );
旧城空念 2024-07-16 13:48:12

您可以将其交换为具有所需容量的新向量。

vector< int > tmp;
old.swap( tmp );

You can swap it with a new vector that has desired capacity.

vector< int > tmp;
old.swap( tmp );
江心雾 2024-07-16 13:48:12

据我所知,你不能将向量重新分配到比以前更低的容量; 你只能分配更大的。 这是有充分理由的; 其中之一是重新分配过程需要大量计算。 如果您确实需要一个较小的向量,请释放旧向量并创建一个较小的新向量。 实际上,这在计算上比将向量实际调整得更小要简单得多。

As far as I can tell, you can't reallocate a vector to a lower capacity than it ever has; you can only allocate it larger. There are good reasons for this; among them is that the reallocation process is hugely computationally intensive. If you really need to have a smaller vector, free the old one and create a new one that's smaller. That's actually computationally much simpler than having the vector actually resize smaller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文