简体   繁体   English

C++:使用 sizeof() 除法查找向量的大小

[英]C++: Using sizeof() division to find the size of a vector

I am trying to get the size of a vector as a number so that I can use it as a constexpr .我正在尝试将向量的大小作为数字获取,以便可以将其用作constexpr

The vector.size() returns a size type that is not a constant expression. vector.size()返回一个不是常量表达式的大小类型。 Therefore, I thought to use sizeof(vector) / sizeof(vector[0]) to get an integer value which I can manually use in a constexpr initialization.因此,我想使用sizeof(vector) / sizeof(vector[0])来获得一个 integer 值,我可以在 constexpr 初始化中手动使用它。

const vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << sizeof(int_vec) / sizeof(int_vec[0]) << endl;

My CLion compiler always prints 6. I do not understand why.我的 CLion 编译器总是打印 6。我不明白为什么。 Any help will be appreciated.任何帮助将不胜感激。

Thanks for the response.感谢您的回复。 I learned that I can use sizeof(arr) / sizeof(arr[0]) to find the length of an array.我了解到我可以使用sizeof(arr) / sizeof(arr[0])来查找数组的长度。 So how would this not work for a vector?那么这对向量如何不起作用?

The reason why vector.size() isn't constexpr is that std::vector grows as you add data to it; vector.size()不是constexpr的原因是std::vector会随着您向其中添加数据而增长; it has a variable size that is not known at compile time.它具有在编译时未知的可变大小。 (That's what constexpr means, that the value of the expression is known at compile time.) (这就是constexpr的意思,表达式的值在编译时是已知的。)

What sizeof (vector) gets you is the size of the in-memory representation of the vector class, which has nothing to do with how many elements are stored in it. sizeof (vector)得到的是vector class 的内存表示的大小,这与其中存储了多少元素无关。 This obviously can't be correct because vector is a class name, not a variable name, so it can't possibly return the size of any one specific vector.这显然是不正确的,因为vector是 class 名称,而不是变量名称,因此它不可能返回任何特定向量的大小。

Using sizeof int_vec , which by the way does not require parentheses, as sizeof is an operator and not a function, is a little more rational, since at least you're asking about the size of some specific vector and not all vectors in general.使用sizeof int_vec ,顺便说一下不需要括号,因为sizeof是一个运算符,而不是 function,更合理一点,因为至少你问的是一些特定向量的大小,而不是一般的所有向量。 But again the problem is that you're going to get the size of the in-memory representation, which has to be fixed.但同样的问题是您将获得内存中表示的大小,这必须被修复。 After all, you can instantiate one on the stack, and so it has to be of some fixed size so that C++ knows how much space to allocate for it.毕竟,您可以在堆栈上实例化一个,因此它必须具有某个固定大小,以便 C++ 知道为它分配多少空间。 It can't just expand in place as you add elements to it, because it would overwrite other data on the stack.它不能在您向其中添加元素时就地扩展,因为它会覆盖堆栈上的其他数据。 It has to have some internal pointer to dynamically-allocated memory for storing the elements.它必须有一些指向动态分配的 memory 的内部指针来存储元素。 When you do sizeof int_vec you're getting the space needed for this pointer and other internal data, not the space needed for the elements themselves.当您执行sizeof int_vec时,您将获得此指针和其他内部数据所需的空间,而不是元素本身所需的空间。

If you want an array-like container that has a size that's fixed at compile time, try std::array .如果您想要一个大小在编译时固定的类数组容器,请尝试std::array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM