矢量下标超出范围和标准 c++ 库超出范围C++

Vector subscript out of range & standard c++ libraries out of range C++

本文关键字:范围 c++ C++ 标准 下标      更新时间:2023-10-16
void pathfind(const int* tiles, int startTile, int endTile, int height) {//const int* tiles is my map
int currentTile = startTile; //The spawn tile

    std::vector<int> points{ startTile };
    int i;//for loops
    bool possible = true;//to make sure they 

    if (tiles[currentTile + 1] <= 2 && floor(currentTile / height) == floor(currentTile - 1 / height)) {//Less than  2 because 3 and up are non-traverseable tiles.
        for (i = 1; i <= points.size(); i++) {
            if (points[i] == currentTile + 1) {
                possible = false;
            }
        }
        if (possible == true) {
            points.resize(points.size() + 1);
            points.at(points.size()) = currentTile + 1;
            currentTile++;
        }
    }
    else if (tiles[currentTile - 1] <= 2 && floor(currentTile / height) == floor(currentTile - 1 / height)) {
        for (i = 1; i <= points.size(); i++) {
            if (points[i] == currentTile - 1) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points.at(points.size()) = currentTile - 1;
                currentTile--;
            }
        }
    }
    else if (tiles[currentTile - height] <= 2) {
        for (i = 1; i <= points.size(); i++) {
            if (points[i] == currentTile - height) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points.at(points.size()) = currentTile - height;
                currentTile--;
            }
        }
    }
    else if (tiles[currentTile + height] <= 2) {
        for (i = 1; i <= points.size(); i++) {
            if (points[i] == currentTile + height) {
                possible = false;
            }
            if (possible == true) {
                points.resize(points.size() + 1);
                points[points.size()] = currentTile + height;
                currentTile--;
            }
        }
    }
    pointList = points;
}

这给了我一个超出范围的运行时错误向量下标,然后如果我忽略它,那么它说站立库超出范围,并且这两个库都在不在文件中的行上。

在此之前,它给了我关于使用的错误

 points.at[];

所以当我删除这些时,这些错误就来了。

我也有 sfml 如果这很重要。

提前谢谢。

C++有效数组的索引(下标)在 [0, array_size-1] 范围内。因此,在访问 tilespoints 元素之前,请确保索引有效。

要在向量末尾添加新元素,请使用push_back()方法:points.push_back(currentTile - 1);