将数据输入向量会生成“调试断言失败!“矢量下标超出范围”

Inputting data into a vector produces "Debug Assertion Failed!" "Vector subscript out of range"

提问人:interben1234 提问时间:12/2/2020 更新时间:12/2/2020 访问量:34

问:

在我的代码中,用户将输入 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);
}
C++ 矢量 按引用传递

评论

0赞 user4581301 12/2/2020
一些有用的读物可以解决未来的错误:为什么 iostream::eof 在循环条件中(即 while (!stream.eof()))被认为是错误的?

答:

1赞 463035818_is_not_an_ai 12/2/2020 #1

在里面声明:createMazemaze

vector<vector<char>> maze;

这是一个空向量。

然后,尝试分配给不存在的元素:

 maze[row][column] = data;

由于此时仍为空,因此任何索引都是越界的。maze

您可以通过调用相应的构造函数来创建具有所需大小的向量:

auto maze = std::vector<vector<char>>(row,std::vector<char>(columns));

这导致了越界错误,但我在您的代码中看到了更多问题。例如,在一些地方,当引用没问题(也许副本错误)时,您传递了 by 值。maze