vector::resize() 和 vector::reserve() 之间的选择

Choice between vector::resize() and vector::reserve()(vector::resize() 和 vector::reserve() 之间的选择)

本文介绍了vector::resize() 和 vector::reserve() 之间的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 vector 成员变量预先分配一些内存.下面的代码是最小的部分

I am pre-allocating some memory to my a vector member variable. Below code is minimal part

class A {
  vector<string> t_Names;
public:
  A () : t_Names(1000) {}
};

现在在某个时间点,如果 t_Names.size() 等于 1000.我打算将大小增加 100.然后如果达到1100,再增加100,以此类推.

Now at some point of time, if the t_Names.size() equals 1000. I am intending to increase the size by 100. Then if it reaches 1100, again increase by 100 and so on.

我的问题是,在 vector::resize()vector::reserve() 之间如何选择.这种情况还有更好的选择吗?

My question is, what to choose between vector::resize() and vector::reserve(). Is there any better choice in this kind of scenario ?

编辑:我对 t_Names 有某种精确的估计.我估计它在 700800 左右.但是在某些(很少)情况下,它可以增长超过 1000.

Edit: I have sort of precise estimate for the t_Names. I estimate it to be around 700 to 800. However in certain (seldom) situations, it can grow more than 1000.

推荐答案

这两个函数做的事情大不相同!

The two functions do vastly different things!

resize() 方法(并将参数传递给构造函数等价于)将向向量插入或删除适当数量的元素以使其给定大小(它具有可选的第二个参数来指定它们的值).它会影响 size(),迭代将遍历所有这些元素,push_back 将在它们之后插入,您可以使用 operator[] 直接访问它们.

The resize() method (and passing argument to constructor is equivalent to that) will insert or delete appropriate number of elements to the vector to make it given size (it has optional second argument to specify their value). It will affect the size(), iteration will go over all those elements, push_back will insert after them and you can directly access them using the operator[].

reserve() 方法只分配内存,但让它未初始化.它只影响 capacity(),但 size() 不会改变.对象没有价值,因为没有向向量添加任何内容.如果然后插入元素,则不会发生重新分配,因为它是提前完成的,但这是唯一的效果.

The reserve() method only allocates memory, but leaves it uninitialized. It only affects capacity(), but size() will be unchanged. There is no value for the objects, because nothing is added to the vector. If you then insert the elements, no reallocation will happen, because it was done in advance, but that's the only effect.

所以这取决于你想要什么.如果您想要一个包含 1000 个默认项的数组,请使用 resize().如果您想要一个数组,您希望在其中插入 1000 个项目并希望避免多次分配,请使用 reserve().

So it depends on what you want. If you want an array of 1000 default items, use resize(). If you want an array to which you expect to insert 1000 items and want to avoid a couple of allocations, use reserve().

Blastfurnace 的评论让我再次阅读了问题并意识到,在您的情况下,正确答案是不要手动预分配.只需根据需要在最后插入元素即可.向量将根据需要自动重新分配,并且比提到的手动方式更有效地.reserve() 唯一有意义的情况是,当您对需要的总大小进行合理精确的估计时,可以轻松地提前获得.

Blastfurnace's comment made me read the question again and realize, that in your case the correct answer is don't preallocate manually. Just keep inserting the elements at the end as you need. The vector will automatically reallocate as needed and will do it more efficiently than the manual way mentioned. The only case where reserve() makes sense is when you have reasonably precise estimate of the total size you'll need easily available in advance.

广告问题如果您有初步估计,则 reserve() 该估计.如果结果还不够,就让向量来做它的事情.

Ad question edit: If you have initial estimate, then reserve() that estimate. If it turns out to be not enough, just let the vector do it's thing.

这篇关于vector::resize() 和 vector::reserve() 之间的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:vector::resize() 和 vector::reserve() 之间的选择