C++ sizeof() 一个向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sizeof() a vector
提问by peterJk
I have a vector<set<char> >
data structure (transactions database) and I want to know the size of it. When I use sizeof() with each set<char>
the size is 24 in spite of the set contains 3, 4 or 5 chars. Later, when I use sizeof() with the vector<set<char> >
the size is 12... I suppose this is not the way to know the size of a data structure. Any help?
Thanks.
我有一个vector<set<char> >
数据结构(交易数据库),我想知道它的大小。当我使用 sizeof() 时set<char>
,尽管集合包含 3、4 或 5 个字符,但每个大小为 24。后来,当我使用 sizeof() 时,vector<set<char> >
大小为 12 ......我想这不是知道数据结构大小的方法。有什么帮助吗?谢谢。
回答by
You want vector::size()
and set::size()
.
你想要vector::size()
和set::size()
。
Assuming v
is your vector, do this:
假设v
是您的向量,请执行以下操作:
size_t size = 0;
for (vector<set<char> >::const_iterator cit = v.begin(); cit != v.end(); ++cit) {
size += cit->size();
}
sizeof()
is giving you the in-memory size of the object/type it is applied to, in multiples of sizeof(char)
(usually one byte). If you want to know the in-memory size of the container and its elements, you could do this:
sizeof()
正在为您提供它所应用的对象/类型的内存大小,以倍数sizeof(char)
(通常是一个字节)为单位。如果你想知道容器及其元素的内存大小,你可以这样做:
sizeof(v) + sizeof(T) * v.capacity(); // where T is the element type
sizeof(v) + sizeof(T) * v.capacity(); // where T is the element type
回答by Andrey
sizeof
returns size of object itself. if it contains pointer to array for example it will not count size of array, it will count only size of pointer (4 on 32 bits) for vector use .size
sizeof
返回对象本身的大小。例如,如果它包含指向数组的指针,它将不计算数组的大小,它将只计算指针的大小(32 位上的 4 个)以供向量使用.size
回答by Stephen Doyle
Vector is implemented using internal pointers to the actual storage. Hence sizeof() will always return the same result which does not include the data storage itself. Try using the vector::size()
method instead. This will return the number of elements in the vector.
Vector 是使用指向实际存储的内部指针实现的。因此 sizeof() 将始终返回相同的结果,其中不包括数据存储本身。请尝试使用该vector::size()
方法。这将返回向量中的元素数。
回答by Nicolás
sizeof()
is computed at compile time, so there is no way it can tell you how many elements it has inside.
sizeof()
是在编译时计算的,所以它无法告诉你它里面有多少个元素。
Use the size()
method of the vector object.
使用size()
vector对象的方法。
回答by Rajendra Uppal
vector
in STL is a class template, when you give template parameter inside <SomeType>
following vector, C++ compiler generated code for a class of type SomeType. So when you populate the vector using push_back
, you are actually inserting another object of SomeType
so when you request .size()
from the compiler it gives you the number of SomeType
objects inserted by you.
Hope that helps!
vector
在 STL 中是一个类模板,当您在<SomeType>
以下向量中提供模板参数时,C++ 编译器会为SomeType类型的类生成代码。因此,当您使用 填充向量时push_back
,您实际上是在插入另一个对象,SomeType
因此当您.size()
从编译器请求时,它会为您提供SomeType
插入的对象数量。
希望有帮助!
回答by Nikolai Fetissov
Use vector::size()
member function to find out number of items in the vector. Hint - they are allocated on the free store.
使用vector::size()
成员函数找出向量中的项目数。提示 - 它们分配在免费商店中。