c++ - 创建巨大矩阵时的问题

标签 c++

我正在尝试创建一种包含随机字符的表格并将其写入文件,但我遇到了问题。当我想创建大表时出现问题。例如,对于一个有 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/

相关文章:

c++ - 如何在 Qt Designer UI 文件中清除 QMainWindow 的几何标签

c++ - 自定义大括号初始值设定项

c++ - std::map.count 使用 c 字符串不起作用?

c++ - 同级友元运算符似乎不参与重载决议

c++ operator<<的多重定义

c++ - 提升 C++ 与 Python 的能力

c++ - 为什么 Eclipse CDT 代码格式化程序有时会在模板参数中引入空格?

c++ - 以结构作为 boost::unordered_map 中的键的 boost 变体

C++ 删除 vector 的某些元素

c++ - 在 VC++ 2010 中将数组作为参数传递时出现编译器错误