我正在尝试收集大量随机数来进行算法分析。然后我遇到了这个我无法解释的问题。考虑以下代码:
#include <exception>
#include <iostream>
#include <deque>
typedef unsigned long long mytype;
const mytype SIZE = 150000000;
int main()
{
std::deque<mytype> rand;
try
{
for (mytype i = 0; i< SIZE; i++)
{
rand.push_back(1); //Just push a dummy number into the deque
}
}
catch (std::exception& e)
{
{
std::cout << e.what() << std::endl;
}
}
return 0;
}
这将导致错误的分配异常。问题是如果我使用 vector 和 reserve() 它将起作用。如果我没有正确理解,请纠正我,deque 不是那种容量更大的数据结构,因为它不像向量那样连续分配内存吗?
我在 Win8 x64、Visual Studio 2012、带有 8G RAM 的英特尔 i7 上运行它。感谢您分享您的想法