对象指针的 C++ vector - max_size()

标签 c++ vector max-size

我有一个类苹果

class apples
{
  private:
      double x;
      double y;
      double z;
  public:
      //some methods
};

我想将指向 apples 对象的指针存储在一个 vector 中。 我这样做是为了在任何文件中创建任何对象并在任何文件中使用任何对象。 我使用以下代码来确定我可以存储在该 vector 中的最大指针数

int _tmain(int argc, _TCHAR* argv[])
{
vector<apples *> myvector;
cout<<"max :"<<myvector.max_size();
return 0;
}

它给了我:

1073741823

现在,我的问题是我真的可以在该 vector 中存储 1073741823 个指针吗,或者这是 vector 的内存限制(即 1073741823 字节)?

所以如果有2个 vector

vector<int> A
& 
vector<double> B

A 可以有 1073741823 个元素 & B 也可以有 1073741823 个元素吗? 我要求澄清这一点, vector 可以存储的最大元素数不取决于存储的实体类型(int 或 double)? (这与vector的当前容量无关!) 另外,指向 apples 对象的指针的大小是多少(不是询问 apples 对象的大小!)? 谢谢。

最佳答案

这是在 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.

因此,您不能存储超过它的数量(实际上由于系统限制可能少于它)

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

并且根据头文件中关于 max_size() 的注释:返回最大可能的 %vector 的大小。

关于对象指针的 C++ vector - max_size(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7874464/

相关文章:

java - 可以通过 TCP 套接字发送的最大字符大小

c++ - 使用 GLM 在 OpenGL 中面向原点时围绕原点旋转对象?

.net - 根据角度计算从矩形中的点到边缘的向量

python - 具有超时、最大大小和连接池的 http 请求

c++ - 计算 vector 中的反转次数

c++ - 我是否正确删除了指向对象的指针 vector ?

php - $ _FILES ['file'] ['size']对于上载超过max_filesize的文件,回显0(零)

c++ - 无法从 C++ 中的 vector 中删除 void 指针

c++ - 使用 Python 的 Matplotlib 绘制在 C++ 程序中生成的数据

c++ - 使用 C++/C 的安全网络编程