简体   繁体   English

向量下标超出范围,带有嵌套的for循环

[英]Vector subscript out of range with nested for loops

below is a snippet of code from a game I am writing in c++ with SFML. 下面是我用SFML用c ++编写的游戏的代码片段。

    if (Keyboard::isKeyPressed(Keyboard::Space)) {
        if (fireClock.getElapsedTime().asMilliseconds() > 150) {
        Bullet vecBullet(Vector2f(player->getPosition()), player->angle);
        bulletVector.push_back(vecBullet);
        sleep(milliseconds(5));
        fireClock.restart();
    }
}

if (Keyboard::isKeyPressed(Keyboard::A)) {
    Enemy vecEnemy;
    enemyVector.push_back(vecEnemy);
}

for (int i = 0; i < bulletVector.size(); i++) {
    bulletVector[i].fire();
    bulletVector[i].draw();
}
for (int i = 0; i < enemyVector.size(); i++) {
    enemyVector[i].draw();
}

for (int i = 0; i < bulletVector.size(); i++) {
    if (bulletVector[i].getPosition().x > 2000 || bulletVector[i].getPosition().x < -100 ||
        bulletVector[i].getPosition().y > 1100 || bulletVector[i].getPosition().y < -100) {
        bulletVector.erase(bulletVector.begin() + i);
    }
}

for (int y = 0; y < bulletVector.size(); y++) {
    for (int x = 0; x < enemyVector.size(); x++) {
        if (enemyVector.size() > 0 && bulletVector.size() > 0) {
            if (enemyVector[x].checkColl(Bullet(bulletVector[y]))) {
                bulletVector.erase(bulletVector.begin() + y);
                enemyVector.erase(enemyVector.begin() + x);
            }
        }
    }
}

cout << enemyVector.size() << " " << bulletVector.size() << endl;

What happens is that once the bullet is shot it hits the enemy and sometimes it crashes and throws the following error: 发生的情况是,一旦子弹被发射,它就会击中敌人,有时会坠毁并引发以下错误:

program: C:\\WINDOWS\\SYSTEM32\\MSVCP140D.dll File: c:\\program files (x86)\\microsoft visualstudio\\2017\\community\\vc\\tools\\msvc\\14.13.26128\\include\\vector Line: 1806 程序:C:\\ WINDOWS \\ SYSTEM32 \\ MSVCP140D.dll文件:c:\\ program files(x86)\\ microsoft visualstudio \\ 2017 \\ community \\ vc \\ tools \\ msvc \\ 14.13.26128 \\ include \\ vector行:1806

Expression: vector subscript out of range 表达式:向量下标超出范围

Any insights you can give me would be greatly appreciated, Thank you. 谢谢您能给我的任何见解。

As @Igor Tandetnik pointed out, you could be deleting an element from your array while iterating with bulletVector.erase(bulletVector.begin() + y); 正如@Igor Tandetnik指出的那样,您可以在使用bulletVector.erase(bulletVector.begin() + y); 进行迭代从数组中删除一个元素 bulletVector.erase(bulletVector.begin() + y);

There are plenty of examples on SO about this topic , and I probably won't explain it better. 关于这个 主题 ,有很多关于SO的例子 ,我可能不会对其做更好的解释。


I'd like to point something extra in your code. 我想在您的代码中指出一些额外的内容。 In this section: 在这个部分:

for (int y = 0; y < bulletVector.size(); y++) {
    for (int x = 0; x < enemyVector.size(); x++) {
        if (enemyVector.size() > 0 && bulletVector.size() > 0) {
            if (enemyVector[x].checkColl(Bullet(bulletVector[y]))) {
                bulletVector.erase(bulletVector.begin() + y);
                enemyVector.erase(enemyVector.begin() + x);
            }
        }
    }
}

where you look for collisions, this sentence is redundant: 在寻找冲突的地方,这句话很多余:

if (enemyVector.size() > 0 && bulletVector.size() > 0) {

If any of both vectors is empty, at least one of the for loops won't iterate a single loop, so you could remove that check and save some computation and lines of code. 如果两个向量中的任何一个都为空,则for循环中的至少一个不会迭代单个循环,因此您可以删除该检查并保存一些计算和代码行。

At that point, you ensure that both vectors have at least 1 element inside. 此时,请确保两个向量内部至少包含1个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM