什么时候应该使用vector的reserve()? [英] When should we use reserve() of vector?

查看:37
本文介绍了什么时候应该使用vector的reserve()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是使用resize(),因为我不能使用reserve,因为它给出了错误:向量下标超出范围.当我阅读了有关 resize() 和 reserve() 差异的信息时,我看到了诸如 Reserve() 设置最大值之类的东西.可以分配的元素数量,但 resize() 目前是我们所拥有的.在我的代码中,我知道最大.元素数量但 Reserve() 没有给我任何有用的东西.那么,如何使用reserve()呢?

I always use resize() because I cannot use reserve as it gives error: vector subscript out of range. As I've read info about the differences of resize() and reserve(), I saw things like reserve() sets max. number of elements could be allocated but resize() is currently what we have. In my code I know max. number of elements but reserve() doesn't give me anything useful. So, how can I make use of reserve()?

推荐答案

一个向量有一个 capacity(由 capacity() 和一个 size<返回/em>(由 size() 返回.第一个说明向量可以容纳多少个元素,第二个说明他当前做了多少保持.

A vector has a capacity (as returned by capacity() and a size (as returned by size(). The first states how many elements a vector can hold, the second how many he does currently hold.

resize改变大小,reserve只改变capacity.

另见 resizereserve 文档.

至于用例:假设您事先知道要在 vector 中放入多少个元素,但不想初始化它们 - 这就是 Reserve 的用例.假设你的向量之前是空的;然后,直接在 reserve() 之后,在执行任何 insertpush_back 之前,当然,您可以不直接访问与您一样多的元素保留空间 - 这将触发上述错误(下标超出范围) - 因为您尝试访问的元素尚未初始化;size 仍然是 0.所以向量仍然是空的;但是,如果您选择保留容量以使其高于或等于您的向量将获得的最大大小,您就可以避免昂贵的重新分配;同时,您还将避免(在某些情况下代价高昂)每个调整大小的向量元素的初始化.

As for the use cases: Let's say you know beforehand how many elements you want to put into your vector, but you don't want to initialize them - that's the use case for reserve. Let's say your vector was empty before; then, directly after reserve(), before doing any insert or push_back, you can, of course, not directly access as many elements as you reserved space for - that would trigger the mentioned error (subscript out of range) - since the elements you are trying to access are not yet initialized; the size is still 0. So the vector is still empty; but if you choose the reserved capacity in such a way that it's higher or equal to the maximum size your vector will get, you are avoiding expensive reallocations; and at the same time you will also avoid the (in some cases expensive) initialization of each vector element that resize would do.

另一方面,使用 resize,您可以说:让向量包含我作为参数给出的尽可能多的元素;初始化那些索引超过旧大小的索引,或者删除超过给定新大小的索引.

With resize, on the other hand, you say: Make the vector hold as many elements as I gave as an argument; initialize those whose indices are exceeding the old size, or remove the ones exceeding the given new size.

请注意,reserve永远不会影响当前向量中的元素(如果需要重新分配,它们的存储位置除外——但不会影响它们的值或它们的数量)!这意味着如果向量的大小当前大于您传递给同一向量上的 Reserve 函数的调用的大小,则 Reserve 将什么也不做.

Note that reserve will never affect the elements currently in the vector (except their storage location if reallocation is needed - but not their values or their number)! Meaning that if the size of a vector is currently greater than what you pass to a call to the reserve function on that same vector, reserve will just do nothing.

另见这个问题的答案:vector::resize() 和 vector 之间的选择::reserve()

这篇关于什么时候应该使用vector的reserve()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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