插入第一个对象后,STL 向量的容量(初始大小为 1000)加倍

发布于 2024-12-05 18:37:13 字数 390 浏览 2 评论 0原文

STL向量的容量无缘无故地翻倍。

我创建一个初始大小为 1000 的向量,插入一项。我预计容量仍为 1000。

vector <int> vec(1000);

cout << "vector capacity " << (unsigned int)vec.capacity() << endl;
vec.push_back(11);

cout << "vector capacity " << (unsigned int)vec.capacity() << endl;

输出为: 向量容量 1000

向量容量 2000 -->插入一项后

STL vector's capacity doubles for no (apparent) reason.

I create a vector with an initial size of 1000, insert one item. I would expect the capacity to remain 1000.

vector <int> vec(1000);

cout << "vector capacity " << (unsigned int)vec.capacity() << endl;
vec.push_back(11);

cout << "vector capacity " << (unsigned int)vec.capacity() << endl;

The output is:
vector capacity 1000

vector capacity 2000 --> after inserting one item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

憧憬巴黎街头的黎明 2024-12-12 18:37:13

std::vector 的构造函数创建一个包含 1000 个元素的向量,并使用默认值(在本例中为 0)进行初始化。它不会创建一个空向量,其中包含稍后要附加的 1000 个元素的空间。

然后添加一个附加元素,因此向量的 size() 现在为 1001。由于它需要重新分配,因此它会将分配的容量加倍,以便分摊后面的 push_back() 的容量。

The constructor for std::vector creates a vector with 1000 elements initialized with the default value, in this case 0. It does NOT create an empty vector with space for 1000 elements to be appended later.

Then you add an additional element, and thus the size() of the vector is now 1001. Since it needs to reallocate, it doubles the allocated capacity, in order to amortize latter push_back()'s.

他不在意 2024-12-12 18:37:13

我预计容量仍为 1000。

大小从 1000 开始,因此容量必须至少为 1001。

至于加倍,那是因为 vector动态阵列,并且每次威胁时将容量加倍<代码>尺寸()>弹出的capacity()确保你得到摊销O(1)push_back。引用维基百科:

当插入n个元素时,容量形成几何级数。按任何常数比例扩展数组可确保插入 n 个元素总共需要 O(n) 时间,这意味着每次插入都需要摊销常数时间。这个比例的值a导致时空权衡:每次插入操作的平均时间约为a/(a−1) ,而浪费细胞的数量以 (a−1)n 为界。

I would expect the capacity to remain 1000.

The size starts out at 1000, so the capacity would have to be at least 1001.

As for the doubling, that's because a vector is a dynamic array, and doubling the capacity every time that the threat of size() > capacity() pops up makes sure you get amortized O(1) push_back. To cite the Wikipedia:

As n elements are inserted, the capacities form a geometric progression. Expanding the array by any constant proportion ensures that inserting n elements takes O(n) time overall, meaning that each insertion takes amortized constant time. The value of this proportion a leads to a time-space tradeoff: the average time per insertion operation is about a/(a−1), while the number of wasted cells is bounded above by (a−1)n.

慢慢从新开始 2024-12-12 18:37:13

首先,不要混淆 size()capacity()

  • size() 给出向量包含的元素数量。
  • capacity() 给出当前保留的内存槽的数量(向量保留的内存中适合多少个元素)。容量始终等于或大于尺寸。

现在,向量vec(1000); 创建一个大小 1000 的向量,因此添加一个元素将得到一个大小为 1001 的向量。

另一方面,容量由向量自动处理以及如何实现,取决于实施情况。典型的方法是每当向量必须增长时将容量加倍,因此实际上,添加新元素的平均时间保持不变,无论向量中有多少项。

First of all, don't confuse size() and capacity().

  • size() gives you the number of elements the vector contains.
  • capacity() gives the current number of reserved memory slots (how many elements fit in the memory the vector has reserved). The capacity is always equal or bigger than the size.

Now, vector<int> vec(1000); creates a vector of size 1000, so adding one element will give you a vector of size 1001.

The capacity, on the other hand, is automatically handled by the vector, and how, depends on the implementation. The typical way of doing it is to double the capacity whenever the vector has to grow, so in effect, the mean time to add new elements remains constant, no matter how many items there are in the vector.

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