空向量是其结构的大小吗? [英] Is an empty vector the size of its struct?
问题描述
假设我有一个这样的结构:
Say I have a struct like this:
struct vertexNodeInfo
{
unsigned char level;
int node;
double leaf;
};
如果我当时有这个:
vector<vertexNodeInfo> node;
在任何 push_back
之前,空向量有多大(内存方面,不是 .size
)?它是否与 vector<int> 的大小(同样,在内存方面)完全相同?节点;
?
How big (memory-wise, not .size
) would the empty vector be, before any push_back
? Would it be exactly the same size (again, in terms of memory) as vector<int> node;
?
推荐答案
没有要求.更重要的是,这是一个表述不佳的问题.
There's no requirement. Even more, it's a poorly formulated question.
向量的 .size
将是 0
,因为它没有元素.
The .size
of the vector would be 0
, because it has no elements.
sizeof
不会影响元素的数量,并且通常足够大以包含一两个指向该类型的指针(它们本身的大小可能会有所不同,因此不能保证 sizeof(vector
和 sizeof(vector
相等),大小和容量.
The sizeof
isn't affected the number of elements, and is typically large enough to contain one or two of pointers to the type (which can vary in size themselves, so there's no guarantee the sizeof(vector<vertexNodeInfo>)
and sizeof(vector<int>)
are equal), the size and capacity.
这篇关于空向量是其结构的大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!