【发布时间】:2020-07-25 13:45:44
【问题描述】:
测试两个向量是否相同
见下方代码
#include <iostream>
#include <vector>
#include <string>
int main(void) {
std::vector<std::string> vstr1(131, "asdf");
std::vector<std::string> vstr2(33131, "asdf");
std::cout << (vstr1 == vstr2) << std::endl;;
std::cout << "******************************" << std::endl;
return 0;
}
效果很好。
现在我将 vstr2 的大小改为非常large,例如 33333333333131
#include <iostream>
#include <vector>
#include <string>
int main(void) {
std::vector<std::string> vstr1(131, "asdf");
std::vector<std::string> vstr2(33333333333131, "asdf");
std::cout << (vstr1 == vstr2) << std::endl;;
std::cout << "******************************" << std::endl;
return 0;
}
不工作,错误信息是
terminate called after throwing an instance of 'std::bad_alloc'
what() std::bad_alloc
Aborted (core dumped) a.out
我已经收集到由于内存分配失败而发生的错误。
如何处理非常大的向量?
【问题讨论】:
-
获取更多内存
-
你认为你为什么需要这么大的向量?
-
致 BessieTheCow :我是一个喜欢尝试语言的新手学习者。也许我不应该太在意细节。
标签: c++ memory vector large-data allocation