c++ - 如何从迷宫 ASCII 文本文件中创建动态分配的二维数组?

标签 c++

我找到了一种将任何 ASCII 文件转换为字符串的简单方法,无论其大小如何,但这对我帮助不大,因为我需要在动态分配的二维数组中使用它?稍后我想将哪些属性转换为图形属性以解决迷宫问题。从我的字符串中获取动态分配的 2D 数组的最佳方法是什么?我希望能够将 2d 数组中的空白转换为顶点,并将它们与边连接起来,在那里我将有一个开始和结束的顶点。

std::ifstream in("d:\\mazes\\mymaze.txt");
std::string s((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
cout << s;

最佳答案

这应该有效:

int rows = 4;//When you change these the array will change size
int cols = 4;

// declaration
int ** a;

// allocate
a = new int*[rows];
for(int i = 0; i < rows; i++)
    a[i] = new int[cols];

// set the values
for(int j = 0; j < rows; j++)
    for(int i = 0; i < rows; i++)
        a[i][j] = 0;

// destruct
for(int i = 0; i < rows; i++)
    delete a[i];
delete a[];

关于c++ - 如何从迷宫 ASCII 文本文件中创建动态分配的二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800869/

相关文章:

c++ - 错误 : enum was not declared in this scope

c++ - TRegEx::IsMatch 始终为假

c++ - 缺少 'printout' CLIPS 的函数声明

c++ - 右值限定方法和 const 表达式

c++ - 混淆条件的评估和 for 循环的步骤

c++ - 条件下移位操作的使用说明

c++ - 在 C/C++ 中迭代 ndarray 列

c++ - 是否有一个很好的 Unix 命令来转储二进制文件的文本表示?

c++ - 使用文字初始化 System::String^,但不使用 string::c_str() 或 char[]

c++ - 检测笔记本电脑插接或未插接 Windows 7+