矢量下标超出范围和标准 c++ 库超出范围C++
Vector subscript out of range & standard c++ libraries out of range C++
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] 范围内。因此,在访问 tiles
或 points
元素之前,请确保索引有效。
要在向量末尾添加新元素,请使用push_back()
方法:points.push_back(currentTile - 1);
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 如何访问超出其块范围的对象?
- 矢量下标超出SFML游戏中的范围