STL resize() 的优点

发布于 2024-08-20 18:59:25 字数 762 浏览 6 评论 0原文

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 技术交流群。

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

发布评论

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

评论(5

忆依然 2024-08-27 18:59:25

听起来好像你应该使用 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.

一桥轻雨一伞开 2024-08-27 18:59:25

请记住替代方案 - 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 with push_back. Use reserve if you want to prepare the array for push_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.

勿忘初心 2024-08-27 18:59:25

resize() 方法更改矢量的 size< /code>,与向量的 capacity 不同

了解这两个值之间的区别非常重要:

  • size 是向量包含的实际元素的数量。
  • 容量是向量在不重新分配更大内存块的情况下可以包含的最大元素数。

向量的容量始终大于或等于其大小。向量的容量永远不会收缩,即使您减小其大小,但有一个例外:当您使用swap()与另一个向量交换内容时向量。正如其他人所提到的,您可以通过调用 reserve() 来增加向量的容量

我认为使用正确的 sizecapacity 术语可以更轻松地理解 C++ vector 类并清楚地说明其行为。

The resize() method changes the vector's size, which is not the same as the vector's capacity.

It is very important to understand the distinction between these two values:

  • The size is the number of actual elements that the vector contains.
  • The 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 its size. A vector's capacity never shrinks, even when you reduce its size, with one exception: when you use swap() to exchange the contents with another vector. And as others have mentioned, you can increase a vector's capacity by calling reserve().

I think that using the correct terminology for size and capacity makes it easier to understand the C++ vector class and to speak clearly about its behavior.

千纸鹤 2024-08-27 18:59:25

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.

鹿港巷口少年归 2024-08-27 18:59:25

我真的不明白这种混乱。 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 of push_backs 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.

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