C++指向对象的指针向量 - max_size()
我有一个 apples 类,
class apples
{
private:
double x;
double y;
double z;
public:
//some methods
};
我想将指向 apples 对象的指针存储在向量中。 我这样做是为了在任何文件中创建任何对象&使用任何文件中的任何对象。 我使用以下代码来确定我可以在它给我的向量中存储的最大指针数
int _tmain(int argc, _TCHAR* argv[])
{
vector<apples *> myvector;
cout<<"max :"<<myvector.max_size();
return 0;
}
:
1073741823
现在,我的问题是我真的可以在该向量中存储 1073741823 个指针还是这是内存限制(即 1073741823 字节)的向量?
所以如果有 2 个向量
vector<int> A
&
vector<double> B
A 可以有 1073741823 个元素 & B也有1073741823个元素吗? 我问这个是为了澄清这一点, 向量可以存储的最大元素数不依赖于存储的实体类型(int 或 double)? (这与向量的当前容量无关!) 另外,指向 apples 对象的指针的大小是多少(不是询问 apples 对象的大小!)? 谢谢。
I have a class apples
class apples
{
private:
double x;
double y;
double z;
public:
//some methods
};
I want to store the pointer to apples objects in a vector.
I am doing this so that i create any object in any file & use any object in any file.
I used the following code to determine the max no of pointers i can store in that vector
int _tmain(int argc, _TCHAR* argv[])
{
vector<apples *> myvector;
cout<<"max :"<<myvector.max_size();
return 0;
}
it gave me:
1073741823
now, my question is that can i really store 1073741823 no of pointers in that vector or is this the memory limitation (i.e. 1073741823 bytes) of the vector?
so if there are 2 vectors
vector<int> A
&
vector<double> B
can A have 1073741823 elements & B also have 1073741823 elements?
i am asking this to clarify that,
the max no of elements that a vector can store does not depend on the the type of entity (int or double) being stored?
(this has nothing to do with the current capacity of the vector!)
Also, what would be the size of a pointer to apples object (not asking the size of apples object!)?
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是在向量中存储元素的库限制。
vector::max_size()
:因此,您不能存储比它更多的内容(实际上,由于系统限制,可能会小于它)
换句话说,即使您拥有最好的资源(内存、CPU...)能力并且您的元素有最小大小,不能存储超过
max_size()
并且根据头文件中
max_size()
的注释: 返回元素的大小最大可能的%向量。This is library limitation for storing elements in a vector.
vector::max_size()
:So, you can not store more than it (and in practice maybe less than it due to system limitations)
In other words even if you have best resource (memory, CPU, ...) abilities and your elements have minimum size, you can not store more than
max_size()
And according to the comment on
max_size()
in the header file: Returns the size of the largest possible %vector.max_size
返回向量可以容纳的元素数量的上限。这并不意味着v.resize(v.max_size())
会成功。它仅仅意味着v.resize(v.max_size() + 1)
将失败。max_size
returns an upper bound on the number of elements the vector can hold. This doesn't mean thatv.resize(v.max_size())
will succeeds. It merely means thatv.resize(v.max_size() + 1)
will fail.我确信一些语言律师会过来指出这是不正确的,但实际上:32 位构建将使用 32 位指针,因此指向对象的指针是四个字节。在 64 位版本中,每个指针可获得 8 个字节。
I'm sure some language lawyer will come around and point out that this is not correct, but in practice: 32bit builds will use 32 bit pointers, so a pointer to an object is four bytes. In 64bit builds, you get eight bytes per pointer.
如果不受任何资源限制(CPU、内存、时间)的限制,向量将无法再向自身添加更多项目。
主要由于内存限制,实际可以存储的数量通常要低得多。
可以通过调用
vector::capacity()
找到当前容量,如果您想了解在当前设置下可以将矢量拉伸多远,请不断调整矢量大小,直到抛出bad_alloc
异常)It is the size at which, if not bound by any resource limits (CPU, memory, time), the vector will no longer be able to add any more items to itself.
The amount you can actually store is generally substantially lower due to mainly memory constraints.
The current capacity can be found by calling
vector::capacity()
, and if you'd like to find out how far you can stretch the vector on your current setup, then keep resizing the vector until abad_alloc
exception is thrown)max_size()
告诉您此vector
实现无法处理大于该值的大小;如果有足够的内存来容纳向量,则向量可以达到该大小。这取决于向量的实现及其使用的分配器。在实践中通常会;您可能会发现它等于
numeric_limits::max() / sizeof (value_type)
;可以在数组中分配的最大字节数除以对象大小。这取决于您的平台。由于
max_size()
太小,我猜测您使用的是 32 位平台,在这种情况下,指针将为 32 位(4 字节)。其他平台将具有不同的指针大小。max_size()
tells you that this implementation ofvector
can't handle a size larger than that; vectors up to that size are possible, if there is enough memory available to contain them.That depends on the implementation of both
vector
, and the allocator it uses. In practice it usually will; you'll probably find that it's equal to something likenumeric_limits<size_t>::max() / sizeof (value_type)
; the maximum number of bytes that can be allocated in an array, divided by the object size.That depends on your platform. Since
max_size()
is so small, I'm guessing you're using a 32-bit platform, in which case a pointer will be 32 bits (4 bytes). Other platforms will have different pointer sizes.hex(1073741823) = '0x3fffffff'
所以这看起来像是一些预定义的限制。
hex(1073741823) = '0x3fffffff'
So that looks like some pre-defined limit.