C++指向对象的指针向量 - max_size()

发布于 2024-12-11 17:10:01 字数 838 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失退 2024-12-18 17:10:02

这是在向量中存储元素的库限制。

vector::max_size()

返回向量容器可以容纳的最大元素数
坚持住。

这不是当前分配给的存储空间量
向量(这可以通过成员向量::容量获得),但是
由于系统或库的原因,向量可能达到的最大潜在大小
实施限制。

因此,您不能存储比它更多的内容(实际上,由于系统限制,可能会小于它)

换句话说,即使您拥有最好的资源(内存、CPU...)能力并且您的元素有最小大小,不能存储超过 max_size()

并且根据头文件中 max_size() 的注释: 返回元素的大小最大可能的%向量。

This is library limitation for storing elements in a vector.

vector::max_size() :

Returns the maximum number of elements that the vector container can
hold.

This is not the amount of storage space currently allocated to the
vector (this can be obtained with member vector::capacity), but the
maximum potential size the vector could reach due to system or library
implementation limitations.

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.

雪落纷纷 2024-12-18 17:10:02

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 that v.resize(v.max_size()) will succeeds. It merely means that v.resize(v.max_size() + 1) will fail.

ぶ宁プ宁ぶ 2024-12-18 17:10:02

此外,指向 apples 对象的指针的大小是多少(不是询问 apples 对象的大小!)?

我确信一些语言律师会过来指出这是不正确的,但实际上:32 位构建将使用 32 位指针,因此指向对象的指针是四个字节。在 64 位版本中,每个指针可获得 8 个字节。

Also, what would be the size of a pointer to apples object (not asking the size of apples object!)?

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.

明月松间行 2024-12-18 17:10:02

如果不受任何资源限制(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 a bad_alloc exception is thrown)

恬淡成诗 2024-12-18 17:10:02

我真的可以在该向量中存储 1073741823 个指针吗?或者这是该向量的内存容量?

max_size() 告诉您此 vector 实现无法处理大于该值的大小;如果有足够的内存来容纳向量,则向量可以达到该大小。

向量中可存储的实体的最大数量是否取决于实体的类型?如果是,如何计算?

这取决于向量的实现及其使用的分配器。在实践中通常会;您可能会发现它等于 numeric_limits::max() / sizeof (value_type);可以在数组中分配的最大字节数除以对象大小。

此外,指向 apples 对象的指针的大小是多少(不是询问 apples 对象的大小!)?

这取决于您的平台。由于 max_size() 太小,我猜测您使用的是 32 位平台,在这种情况下,指针将为 32 位(4 字节)。其他平台将具有不同的指针大小。

can i really store 1073741823 pointers in that vector or is this the memory capacity of the vector?

max_size() tells you that this implementation of vector can't handle a size larger than that; vectors up to that size are possible, if there is enough memory available to contain them.

does the max no of entities store able in a vector depend on the type of entity? if yes, how to calculate that?

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 like numeric_limits<size_t>::max() / sizeof (value_type); the maximum number of bytes that can be allocated in an array, divided by the object size.

Also, what would be the size of a pointer to apples object (not asking the size of apples object!)?

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.

榆西 2024-12-18 17:10:02

hex(1073741823) = '0x3fffffff'

所以这看起来像是一些预定义的限制。

hex(1073741823) = '0x3fffffff'

So that looks like some pre-defined limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文