迭代并从 Vector 中删除元素.错误:向量迭代器不可递增 [英] Iterating over and removing elements from a Vector. Error:Vector iterator not Incrementable

查看:62
本文介绍了迭代并从 Vector 中删除元素.错误:向量迭代器不可递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历存储的 std::unique_ptr 向量,以检查它们是否与 player 的攻击发生冲突.如果是,我想从向量中删除 Enemy.但是,当我这样做时,我收到错误:

I'm trying to iterate over a vector of stored std::unique_ptr<Enemy>'s to check if they're colliding with the player's attack. If they are, I want to delete the Enemy from the vector. However when I do this, I get the error:

文件:c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector线路:102表达式:向量迭代器不可增加

File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector Line: 102 Expression: vector iterator not incrementable

代码如下:

if(actionCnt > 0)
{
    sf::FloatRect* playerAction = player->action(actionCnt);

    if (!enemies.empty()) {
        for (auto&i : enemies) {
            if (playerAction->intersects(i->getBox())) {
                i->kill();
                enemies.erase(std::remove(enemies.begin(), enemies.end(), i));
            }
        }
    }

    delete playerAction;
    playerAction = NULL;
}

因此,本质上,如果玩家触发攻击,actionCnt 会变为 1,并且该段被执行.

So essentially, if the player triggers an attack, actionCnt goes to 1 and this segment gets executed.

playerAction 是一个被移动以模拟挥杆动作的矩形,所以我在这里检查矩形是否与敌人发生碰撞,如果是这样,敌人会杀死它.

playerAction is a rectangle that gets moved to simulate a swing, so here I'm checking to see if the rectangle collides with an enemy which would kill it if so.

我使用的是 SFML 库,但这些类的作用应该很明显.

I'm using SFML libraries, but it should be obvious what the classes do.

现在我在场上只有 1 个敌人来测试这些东西.所以当我击中它时,enemies 向量应该是空的.

Right now I only have 1 enemy on the field for testing this stuff. So when I hit it, the enemies vector should be empty.

推荐答案

代码中的 auto 将返回向量容器内的类型.它不会给你迭代器.代替基于范围的 for 循环,您可以使用经典的 for 循环并使用迭代器擦除您的元素.

The auto in your code will return the type inside the vector container. It will not give you the iterator. Instead of a range-based for loop, you can use a classic for loop and erase your element with the iterator.

if (!enemies.empty())
{
    for (auto itr = std::begin(enemies); itr!=std::end(enemies); ++itr)
    {
       if ((*itr)->hit)
       {
          (*itr)->kill();
          enemies.erase(itr);
       }
    }
}

或者您可以在 std::remove_if 中使用 lambda 函数,然后在一行中完成整个操作.这实际上被称为 erase-remove idiom(更多 这里).简而言之,remove_if 以保留顺序的方式移动(移动)容器中所有从 lambda 函数返回 false 的元素,并返回一个迭代器,指向最后一个元素之后的元素.从 lambda 函数返回 true 的元素被移动到向量的末尾,并且仍然是可解引用的,但未定义.为了完全擦除元素,调用向量的擦除函数来删除从容器的逻辑端(来自 std::remove_if 的迭代器)到物理端(vector.end())的所有元素.

Or you can a lambda function inside the std::remove_if and do the whole thing in one line. This is actually known as the erase-remove idiom (more here). In a nutshell, the remove_if shifts (moves) all the elements in the container that return false from the lambda function in a preserve-the-ordering fashion, and returns an iterator to the one past the last element. The elements that returned true from the lambda function are moved to the end of the vector and are still dereferencable, yet undefined. To erase the elements completely, the vector's erase function is called to delete all the elements from the logical end (iterator from std::remove_if) to the physical end (vector.end()) of the container.

if (!enemies.empty())
   enemies.erase(std::remove_if(enemies.begin(),enemies.end(),[](const auto &itr){return itr->hit;}),std::end(enemies)); 

此外,当您希望对元素执行操作而不改变容器本身时,将使用基于范围的 for 循环.为了获得更安全的代码,您可以使用 while 循环而不是基于范围的 for 循环.

Furthermore, the ranged-based for loop is used for when you wan to perform an action to your elements without mutating the container itself. For a safer code, you can use a while-loop instead of a range-based for-loop.

if (!enemies.empty())
{
    auto itr = std::begin(enemies); 
    while (itr != std::end(enemies) )
    {
        if ((*itr)->hit)
        {
           (*itr)->kill();
           enemies.erase(itr);
        }
        ++itr;
    } 
}

您可以在此处查看代码:http://rextester.com/EHLB11709我试图模仿你的游戏逻辑,只关注删除部分.从输出中您将看到第 6 个元素已被删除.

You can have a look at the code here: http://rextester.com/EHLB11709 I tried to imitate your game logic focusing only on the delete part. From the output you will see that the 6th element has been deleted.

此外,如果您想从向量中删除所有内容,您应该使用 clear().

Furthermore, if you want to remove everything from your vector, you should use clear().

这篇关于迭代并从 Vector 中删除元素.错误:向量迭代器不可递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆