STL resize() 的优点
resize()
函数使向量包含所需数量的元素。如果我们需要的元素少于向量已包含的元素,则最后的元素将被删除。如果我们要求向量增长,它会扩大其大小并用零填充新创建的元素。
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v[i] = i*2;
}
但是,如果我们在 resize()
之后使用 push_back()
,它将在新分配的大小之后添加元素,但不会添加到其中。上面的例子得到的向量的大小是25,而如果我们在第二次循环中使用push_back(),它会是30。
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v.push_back(i*2); // Writes to elements with indices [25..30), not [20..25) ! <
}
那么resize()函数的优势在哪里呢?它不会对向量中的索引和访问元素造成混乱吗?
The resize()
function makes vector contain the required number of elements. If we require less elements than vector already contain, the last ones will be deleted. If we ask vector to grow, it will enlarge its size and fill the newly created elements with zeroes.
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v[i] = i*2;
}
But if we use push_back()
after resize()
, it will add elements AFTER the newly allocated size, but not INTO it. In the example above the size of the resulting vector is 25, while if we use push_back() in a second loop, it would be 30.
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v.push_back(i*2); // Writes to elements with indices [25..30), not [20..25) ! <
}
Then where is the advantage of resize()
function ? Doesn't it creates a confusion for indexing and accessing elements from the vector ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来好像你应该使用
vector::reserve
。vector::resize
用于<使用给定值(或只是默认值)初始化新创建的空间。函数的第二个参数是要使用的初始化值。It sounds as though you should be using
vector::reserve
.vector::resize
is used to initialize the newly created space with a given value (or just the default.) The second parameter to the function is the initialization value to use.请记住替代方案 -
reserve
。当您想要使用[]
运算符对向量进行操作时,请使用resize
——因此您需要一个“空”元素表。resize
不适用于push_back
。如果您想为push_back
准备数组,请使用reserve
。如果数组具有有意义的“空”构造函数,则调整大小主要有用,此时您可以创建空元素数组,并且仅更改有意义的元素。
Remember the alternative -
reserve
.resize
is used when you want to act on the vector using the[]
operator -- hence you need a "empty" table of elements.resize
is not intended to be used withpush_back
. Usereserve
if you want to prepare the array forpush_back
.Resize is mainly usefull if the array has meaningful "empty" constructor, when you can create an array of empty elements, and only change the ones that are meaningful.
resize()
方法更改矢量的size< /code>
,与向量的
capacity 不同
。了解这两个值之间的区别非常重要:
size
是向量包含的实际元素的数量。容量
是向量在不重新分配更大内存块的情况下可以包含的最大元素数。向量的
容量
始终大于或等于其大小
。向量的容量
永远不会收缩,即使您减小其大小
,但有一个例外:当您使用swap()
与另一个向量交换内容时向量。正如其他人所提到的,您可以通过调用reserve()
来增加向量的容量
。我认为使用正确的
size
和capacity
术语可以更轻松地理解 C++vector
类并清楚地说明其行为。The
resize()
method changes the vector'ssize
, which is not the same as the vector'scapacity
.It is very important to understand the distinction between these two values:
size
is the number of actual elements that the vector contains.capacity
is the maximum number of elements that the vector could contain without reallocating a larger chunk of memory.A vector's
capacity
is always larger or equal to itssize
. A vector'scapacity
never shrinks, even when you reduce itssize
, with one exception: when you useswap()
to exchange the contents with another vector. And as others have mentioned, you can increase a vector'scapacity
by callingreserve()
.I think that using the correct terminology for
size
andcapacity
makes it easier to understand the C++vector
class and to speak clearly about its behavior.resize() 函数通过在向量中插入或删除元素来更改向量的实际内容。它不仅改变了其存储容量。要仅更改存储容量,请改用 vector::reserve。查看链接中的矢量可视化,注意其中v.back 指向。
resize() function changes the actual content of the vector by inserting or erasing elements from the vector. It does not only change its storage capacity. To direct a change only in storage capacity, use vector::reserve instead. Have a look at the vector visualization in the link, notice where v.back is pointing to.
我真的不明白这种混乱。
resize
的优点是它可以调整矢量的大小。必须执行一个push_back
循环既乏味,又可能需要多次“实际”调整大小。如果您想“调整”向量的大小而不更改其可访问索引,请使用
std::vector::reserve
。这将更改内部分配数组的大小,而无需实际“添加”任何内容。I don't really understand the confusion. The advantage of
resize
is that it resizes your vector. Having to do a loop ofpush_back
s is both tedious and may require more than one "actual" resize.If you want to "resize" your vector without changing its accessible indexes then use
std::vector<T>::reserve
. That will change the size of the internal allocated array without actually "adding" anything.