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

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

问题描述

我为一个向量成员变量预分配了一些内存。下面的代码是最小的部分

  class A {
vector< string& t_Names;
public:
A():t_​​Names(1000){}
};

现在在某个时间点,如果 t_Names.size / code>等于 1000 。我打算增加大小 100 。然后,如果达到 1100 ,再次增加 100 ,等等。



我的问题是, vector :: resize() vector :: reserve()。在这种情况下有更好的选择吗?



编辑:我有一个精确的估计为 t_Names 。我估计它在 700 800 。但在某些(很少)情况下,它可以增长超过 1000



resize() 方法(和构造函数的传递参数相当)将插入或删除适当数量的元素到向量使它给定大小(它有可选的第二个参数来指定它们的值)。它将影响 size(),迭代将覆盖所有这些元素,push_back将在它们之后插入,您可以使用运算符[ ]



reserve() 方法只分配内存,但保留未初始化。它只影响 capacity(),但 size()将不会改变。没有对象的值,因为没有添加到向量。如果你插入元素,没有重新分配将发生,因为它是提前完成,但这是唯一的效果。



这取决于你想要什么。如果你想要一个1000个默认项目的数组,使用 resize()。如果你想要一个数组,你希望插入1000项,并希望避免几个分配,使用 reserve()



修改: Blastfurnace的评论让我再次阅读问题,并认识到,在您的情况下,正确的答案是手动预分配。只需根据需要继续插入元素。向量将根据需要自动重新分配,并且将比所提及的手动方式更有效地进行。 reserve()的唯一情况是,当您对总规模进行合理精确的估计时,您需要提前轻松获得。



EDIT2:广告问题编辑:如果您有初始估算,则 reserve()估计。如果事实证明是不够的,只是让向量做它的东西。


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) {}
};

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.

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

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!

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[].

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.

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().

EDIT: 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.

EDIT2: 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()之间的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆