我找到了一种将任何 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/