简体   繁体   English

C ++中向量的大小

[英]vector's size in C++

#include <iostream>
#include <vector>
#include "mixmax.h"//<-here, there is random number generator called  mixmax
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
const unsigned long long int n=10000000;
vector < float > f(n);
vector < float > distance_1(n);
vector < float > distance_2(n);
rng_state_t s;
rng_state_t *x=&s;
seed_spbox(x,12345);//<-here we just devlare our random number generator
for(int i=0;i<n;i++)
f[i]=int(n*get_next_float(x));//,<-here we just get random numbers,like    rand()
sort(f.begin(),f.end());
for(int i=0;i<n;i++)
{
   distance_1[i]=f[i]-i;
   distance_2[i]=(i+1)-f[i];
}
float        discrep=max(*max_element(distance_1.begin(),distance_1.end()),*max_element(dis    tance_2.begin(),distance_2.end()));
cout<<"discrep= "<<discrep<<endl;
cout<<"sqrt(n)*discrep= "<<discrep/sqrt(n)<<endl;
}

When I print f.max_size() (the vector declined above in code) gives me this huge number 4611686018427387903, but when I take n=10000000000, it does not work, it gives this error: 当我打印f.max_size() (代码在上面的向量中下降了)时,给我这个巨大的数字4611686018427387903,但是当我采用n = 10000000000时,它不起作用,则出现此错误:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped). 

(I tried it in Visual Studio under windows.) (我在Windows下的Visual Studio中尝试过。)

What's the problem ??? 有什么问题 ??? If vectors do not work for big sizes, can anyone tell me how can I use vectors or arrays with very big sizes ??? 如果向量不能用于大尺寸,谁能告诉我如何使用大尺寸的向量或数组?

Quoting cplusplus.com , 引用cplusplus.com

std::vector::max_size std :: vector :: max_size

Returns the maximum number of elements that the vector can hold. 返回向量可以容纳的最大元素数。

This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached. 由于已知的系统或库实现限制,这是容器可以达到的最大潜在大小, 但绝不能保证容器能够达到该大小:在达到该大小之前,它仍然无法在任何时候分配存储。

Hence, vector doesn't guarantee that it can hold max_size elements, it just an implementation limitation. 因此, vector不能保证它可以容纳max_size元素,而只是实现限制。

Also, chris mentioned: 克里斯还提到:

Well 10 GB * sizeof(float) * 3 is a ton of memory to allocate. 10 GB * sizeof(float) * 3是要分配的大量内存。 I'm going to guess your OS isn't letting you allocate it all for a good reason. 我猜您的操作系统没有充分的理由让您分配它们。

The OP asks, OP问,

If vectors do not work for big sizes, can anyone tell me how can I use vectors or arrays with very big sizes ??? 如果向量不能用于大尺寸,谁能告诉我如何使用大尺寸的向量或数组?

Yes you can. 是的你可以。 Try Roomy or STXXL . 尝试RoomySTXXL

max_size() is different of size() and is different of capacity() max_size()size()有所不同, capacity()也不相同

Current capacity is n=10000000 so the last element is distance_1[9999999] 当前容量为n=10000000因此最后一个元素为distance_1[9999999]

What's the problem ??? 有什么问题 ???

Presumably, the allocation fails because your computer doesn't have 120GB of memory available. 大概是分配失败,因为您的计算机没有120GB的可用内存。 max_size tells you how many elements the implementation of vector can theoretically manage, given enough memory; max_size告诉您,只要有足够的内存, vector的实现理论上可以管理多少个元素; it doesn't know how much memory will actually be available when you run the program. 它不知道您在运行程序时实际上将有多少可用内存。

If this vectors do not work for big sizes Can anyone tell me how can I use vectors or arrays with big, very big size ??? 如果此向量不适用于大尺寸,谁能告诉我如何使用大或非常大的向量或数组?

Increase the amount of RAM or swap space on your computer (and make sure the OS is 64-bit, though from the value of max_size() I guess it is). 增加计算机上的RAM或交换空间(并确保操作系统为64位,尽管我猜是max_size()的值)。 Or use something like STXXL to use files to back up huge data structures. 或者使用STXXL之类的文件来备份大型数据结构。

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

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