向量的大小与容量?

size vs capacity of a vector?(向量的大小与容量?)
本文介绍了向量的大小与容量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我对此有点困惑,这两者在我看来是一样的.尽管可能会发生容量和大小在不同编译器上有所不同的情况.它可能如何不同.它还说,如果我们的内存不足,容量会发生变化.

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.

这篇关于向量的大小与容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)