将数据输入向量会产生 "Debug Assertion Failed!" "Vector subscript out of range"
Inputting data into a vector produces "Debug Assertion Failed!" "Vector subscript out of range"
在我的代码中,用户将在特定点输入 2 个值,这些值用于将 .txt 文件中的字符存储到向量中。为此,我使用了
maze[row][column] = data;
但是,这会导致“调试断言失败!” “表达式:向量下标超出范围”
请在下面找到我的完整代码,我是使用向量的新手,所以我不太确定为什么这不起作用。
#include<fstream>
#include<iostream>
#include<string>
#include <vector>
//Maze Size
#define SIZE 5
using namespace std;
bool isSafe( vector<vector<char>> maze, vector<vector<int>> visited, int x, int y);
bool isValid(int x, int y, int rows, int columns);
void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist, string& p_route,string route, int rows, int columns);
void userInput();
void createMaze(int rows, int columns, string filename);
int main()
{
userInput();
}
bool isSafe(vector<vector<char>> maze, vector<vector<int>> visited, int x, int y)
{
if (maze[x][y] == 'x' || visited[x][y] || maze[x][y] == NULL)
return false;
return true;
}
bool isValid(int x, int y, int rows, int columns)
{
if (x < rows && y < columns && x >= 0 && y >= 0)
return true;
return false;
}
void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist,string&p_route,string route, int rows, int columns) //I&J Start Point, X&Y End point
{
bool solved = false;
// if destination is found, update min_dist
while (!solved)
{
if (i == x && j == y)
{
p_route = route;
min_dist = min(dist, min_dist);
if (min_dist != INT_MAX)
{
cout << "The shortest path from source to destination has length " << min_dist << endl;
cout << "The route through the maze is: " << p_route << endl;
}
else
{
cout << "Destination can't be reached from given source";
}
solved = true;
return;
}
// set (i, j) cell as visited
visited[i][j] = 1;
// go to bottom cell
if (isValid(i + 1, j, rows, columns) && isSafe(maze, visited, i + 1, j))
findShortestPath(maze, visited, i + 1, j, x, y, min_dist, dist + 1, p_route, route.append("S") , rows, columns);
// go to right cell
if (isValid(i, j + 1, rows, columns) && isSafe(maze, visited, i, j + 1))
findShortestPath(maze, visited, i, j + 1, x, y, min_dist, dist + 1, p_route, route.append("E") , rows, columns);
// go to top cell
if (isValid(i - 1, j, rows, columns) && isSafe(maze, visited, i - 1, j))
findShortestPath(maze, visited, i - 1, j, x, y, min_dist, dist + 1, p_route, route.append("N"), rows, columns);
// go to left cell
if (isValid(i, j - 1, rows, columns) && isSafe(maze, visited, i, j - 1))
findShortestPath(maze, visited, i, j - 1, x, y, min_dist, dist + 1, p_route, route.append("W") , rows, columns);
// Backtrack - Remove (i, j) from visited mazerix
visited[i][j] = 0;
}
}
void userInput()
{
string filename;
int rows;
int columns;
cout << "Please input the name of your maze!" << endl;
cin >> filename;
cout << endl;
cout << "Please input the amount of rows in your maze!" << endl;
cin >> rows;
cout << endl;
cout << "Please input the amount of columns in your maze!" << endl;
cin >> columns;
cout << endl;
createMaze(rows, columns, filename);
}
void createMaze(int rows, int columns, string filename)
{
int startRow = 0;
int startColumn = 0;
int endRow = 0;
int endColumn = 0;
int column = 0;
vector<vector<char>> maze;
ifstream input(filename);
char data = input.get();
string route;
string p_route;
while (!input.eof())
{
for (int row = 0; row < rows; row++)
{
while (data != '\n' && !input.eof())
{
if (data == 'A')
{
startRow = row;
startColumn = column;
}
if (data == 'B')
{
endRow = row;
endColumn = column;
}
maze[row][column] = data;
column++;
data = input.get();
}
column = 0;
data = input.get();
}
}
input.close();
cout << "The Maze being solved is: " << endl;
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < columns; x++)
{
cout << maze[y][x];
}
cout << endl;
}
input.close();
// construct a mazerix to keep track of visited cells
vector<vector<int>> visited;
// initially all cells are unvisited
// memset(visited, 0, sizeof visited);
int min_dist = INT_MAX;
findShortestPath(maze, visited, startRow, startColumn, endRow, endColumn, min_dist, 0, p_route, route, rows,columns);
}
在 createMaze
中声明 maze
:
vector<vector<char>> maze;
这是一个空向量。
然后您尝试分配给不存在的元素:
maze[row][column] = data;
因为 maze
在那一点上仍然是空的,任何索引都超出范围。
您可以通过调用适当的构造函数来创建具有所需大小的向量:
auto maze = std::vector<vector<char>>(row,std::vector<char>(columns));
这是导致越界错误的原因,但我在您的代码中发现了更多问题。例如,在几个地方你按值传递 maze
时引用就可以了(也许副本是错误的)。
在我的代码中,用户将在特定点输入 2 个值,这些值用于将 .txt 文件中的字符存储到向量中。为此,我使用了
maze[row][column] = data;
但是,这会导致“调试断言失败!” “表达式:向量下标超出范围”
请在下面找到我的完整代码,我是使用向量的新手,所以我不太确定为什么这不起作用。
#include<fstream>
#include<iostream>
#include<string>
#include <vector>
//Maze Size
#define SIZE 5
using namespace std;
bool isSafe( vector<vector<char>> maze, vector<vector<int>> visited, int x, int y);
bool isValid(int x, int y, int rows, int columns);
void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist, string& p_route,string route, int rows, int columns);
void userInput();
void createMaze(int rows, int columns, string filename);
int main()
{
userInput();
}
bool isSafe(vector<vector<char>> maze, vector<vector<int>> visited, int x, int y)
{
if (maze[x][y] == 'x' || visited[x][y] || maze[x][y] == NULL)
return false;
return true;
}
bool isValid(int x, int y, int rows, int columns)
{
if (x < rows && y < columns && x >= 0 && y >= 0)
return true;
return false;
}
void findShortestPath(vector<vector<char>> maze, vector<vector<int>> visited, int i, int j, int x, int y, int& min_dist, int dist,string&p_route,string route, int rows, int columns) //I&J Start Point, X&Y End point
{
bool solved = false;
// if destination is found, update min_dist
while (!solved)
{
if (i == x && j == y)
{
p_route = route;
min_dist = min(dist, min_dist);
if (min_dist != INT_MAX)
{
cout << "The shortest path from source to destination has length " << min_dist << endl;
cout << "The route through the maze is: " << p_route << endl;
}
else
{
cout << "Destination can't be reached from given source";
}
solved = true;
return;
}
// set (i, j) cell as visited
visited[i][j] = 1;
// go to bottom cell
if (isValid(i + 1, j, rows, columns) && isSafe(maze, visited, i + 1, j))
findShortestPath(maze, visited, i + 1, j, x, y, min_dist, dist + 1, p_route, route.append("S") , rows, columns);
// go to right cell
if (isValid(i, j + 1, rows, columns) && isSafe(maze, visited, i, j + 1))
findShortestPath(maze, visited, i, j + 1, x, y, min_dist, dist + 1, p_route, route.append("E") , rows, columns);
// go to top cell
if (isValid(i - 1, j, rows, columns) && isSafe(maze, visited, i - 1, j))
findShortestPath(maze, visited, i - 1, j, x, y, min_dist, dist + 1, p_route, route.append("N"), rows, columns);
// go to left cell
if (isValid(i, j - 1, rows, columns) && isSafe(maze, visited, i, j - 1))
findShortestPath(maze, visited, i, j - 1, x, y, min_dist, dist + 1, p_route, route.append("W") , rows, columns);
// Backtrack - Remove (i, j) from visited mazerix
visited[i][j] = 0;
}
}
void userInput()
{
string filename;
int rows;
int columns;
cout << "Please input the name of your maze!" << endl;
cin >> filename;
cout << endl;
cout << "Please input the amount of rows in your maze!" << endl;
cin >> rows;
cout << endl;
cout << "Please input the amount of columns in your maze!" << endl;
cin >> columns;
cout << endl;
createMaze(rows, columns, filename);
}
void createMaze(int rows, int columns, string filename)
{
int startRow = 0;
int startColumn = 0;
int endRow = 0;
int endColumn = 0;
int column = 0;
vector<vector<char>> maze;
ifstream input(filename);
char data = input.get();
string route;
string p_route;
while (!input.eof())
{
for (int row = 0; row < rows; row++)
{
while (data != '\n' && !input.eof())
{
if (data == 'A')
{
startRow = row;
startColumn = column;
}
if (data == 'B')
{
endRow = row;
endColumn = column;
}
maze[row][column] = data;
column++;
data = input.get();
}
column = 0;
data = input.get();
}
}
input.close();
cout << "The Maze being solved is: " << endl;
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < columns; x++)
{
cout << maze[y][x];
}
cout << endl;
}
input.close();
// construct a mazerix to keep track of visited cells
vector<vector<int>> visited;
// initially all cells are unvisited
// memset(visited, 0, sizeof visited);
int min_dist = INT_MAX;
findShortestPath(maze, visited, startRow, startColumn, endRow, endColumn, min_dist, 0, p_route, route, rows,columns);
}
在 createMaze
中声明 maze
:
vector<vector<char>> maze;
这是一个空向量。
然后您尝试分配给不存在的元素:
maze[row][column] = data;
因为 maze
在那一点上仍然是空的,任何索引都超出范围。
您可以通过调用适当的构造函数来创建具有所需大小的向量:
auto maze = std::vector<vector<char>>(row,std::vector<char>(columns));
这是导致越界错误的原因,但我在您的代码中发现了更多问题。例如,在几个地方你按值传递 maze
时引用就可以了(也许副本是错误的)。