c++ - 空 vector 是其结构的大小吗?

标签 c++ vector struct

假设我有一个这样的结构:

struct vertexNodeInfo
{
    unsigned char level;
    int node;
    double leaf;
};

如果我有这个:

vector<vertexNodeInfo> node;

在任何 .size 之前,空 vector 有多大(内存方面,不是 push_back ) ?它的大小是否与 vector<int> node; 完全相同(同样,就内存而言)? ?

最佳答案

没有要求。更重要的是,这是一个表述不当的问题。

.size vector 的大小为 0 ,因为它没有元素。

sizeof不影响元素的数量,并且通常大到足以包含一个或两个指向该类型的指针(它们本身的大小可能会有所不同,因此不能保证 sizeof(vector<vertexNodeInfo>)sizeof(vector<int>) 相等),大小和容量。

关于c++ - 空 vector 是其结构的大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740287/

相关文章:

c++ - 在 C++ 中 : how to print the digits after the decimal.

c++ - 如何为 vector 重载 ostream (<<)(打印 vector 中的所有集合)

C++ 使用 std::list of pointers 重新排序 std::vector 元素

vector - Julia :附加到一个空向量

c# - 使用 PInvoke 时,何时应使用 class,何时应在要求输入指向 NAME 结构的指针时使用 struct?

c - for循环重写结构体C中数组的所有值

c++ - 为什么在上下文转换中不发生显式 bool() 转换

c++ - OpenCV Image Mat 到 1D CHW(RR...R, GG..G, BB..B) vector

c++ - 在MinGW的Shell中编译libtcod时出现 “Error 1”

c - 尝试理解链表和结构