我正在尝试创建一种包含随机字符的表格并将其写入文件,但我遇到了问题。当我想创建大表时出现问题。例如,对于一个有 10 行 x 5 列的表,一切都是完美的,但是对于一个有 1.000.000 行和 100 列的表,我有错误。您知道我该如何解决这个问题吗???
我附上了我创建的代码:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>
#include<sstream>
using namespace std;
int main()
{
int rows, columns, rowsMax, columnsMax;
int element1;
char word[10];
cout<<"Write the number of rows of your table: ";
cin>>rowsMax;
cout<<"Write the number of columns of your table: ";
cin>>columnsMax;
string matriz[100000][5]; // Here I write the number of row and columns that must be the same than the numbers introduced as input
string table ("Table1");
ofstream myfile (table);
if(myfile.is_open())
srand(1);
for(rows=0;rows<rowsMax;rows++)
{
for(columns=0;columns<columnsMax;columns++)
{
element1 = rand() % 100000 + 1;
int len = rand () % 4 + 4;
word [len] = 0;
while (len) word [--len] = 'A' + rand () % 58;
myfile<<element1<<word;
myfile<<"|";
std::stringstream ss;
ss<<element1;
string mat;
mat +=ss.str();
mat +=word;
matriz[rows][columns]= mat;
}
myfile<<endl;
}
myfile.close();
system("pause");
return 0;
}
在此先感谢您的帮助!
最佳答案
您已经在堆栈上分配了通常大小非常有限的数组。对于 Windows,默认情况下约为 1Mb。
您可以在堆上分配数组。
关于c++ - 创建巨大矩阵时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5635997/