size vs capacity of a vector?(向量的大小与容量?)
问题描述
我对此有点困惑,这两者在我看来是一样的.尽管可能会发生容量和大小在不同编译器上有所不同的情况.它可能如何不同.它还说,如果我们的内存不足,容量会发生变化.
I am a bit confused about this both of these look same to me. Although it may happen that capacity and size may differ on different compilers. how it may differ. Its also said that if we are out of memory the capacity changes.
所有这些事情我都不太清楚.
All these things are bit unclear to me.
有人可以解释一下吗.(如果可能的话,或者我可以对任何程序进行任何测试以理解它)
Can somebody give an explanation.(if possible with and example or if I can do any test on any program to understand it)
推荐答案
大小 不允许在多个编译器之间存在差异.向量的大小就是它包含的元素个数,直接由你放入向量中元素的数量来控制.
Size is not allowed to differ between multiple compilers. The size of a vector is the number of elements that it contains, which is directly controlled by how many elements you put into the vector.
Capacity 是向量的总空间量.在引擎盖下,向量只是使用一个数组.向量的容量是该数组的大小.这始终等于或大于大小.它们之间的区别在于在需要重新分配引擎盖下的数组之前可以添加到向量中的元素数量.
Capacity is the amount of total space that the vector has. Under the hood, a vector just uses an array. The capacity of the vector is the size of that array. This is always equal to or larger than the size. The difference between them is the number of elements that you can add to the vector before the array under the hood needs to be reallocated.
您几乎不应该关心容量.它的存在是为了让具有非常具体的性能和内存限制的人做他们想做的事情.
You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want.
这篇关于向量的大小与容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向量的大小与容量?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01