简体   繁体   English

在这种情况下,如何从向量中删除项目?

[英]How do I erase an item from a vector in this situation?

I get a syntax error for everything I try. 我尝试的所有内容都出现语法错误。 Can someone please tell me what to put in place of the comment? 有人可以告诉我用什么代替评论吗? This is my first time working with vectors. 这是我第一次使用向量。

EntityList is a static vector of the Entity class. EntityList是Entity类的静态向量。

for(int i = 0;i < (int)Entity::EntityList.size();i++) {
    if(!Entity::EntityList[i]) continue;

    if(Entity::EntityList[i]->isDead){
        //Erase from vector
        //Decrement i?
    }

    Entity::EntityList[i]->OnLoop();
}

What should I put in place of the comment? 我应该用什么代替评论? I've tried a few things but nothing works. 我已经尝试了一些方法,但是没有任何效果。 For example, Entity::EntityList.erase(i); 例如,Entity :: EntityList.erase(i); doesn't work. 不起作用。 I get the following error, which I don't understand: 我收到以下错误,我不明白:

cannot convert parameter 1 from 'int' to 'std::_Vector_const_iterator<_Myvec>' 无法将参数1从'int'转换为'std :: _ Vector_const_iterator <_Myvec>'

All the examples I see use ints for the parameter, so I don't know what to do. 我看到的所有示例都将ints用作参数,因此我不知道该怎么做。

Also, since elements in the vector are shifted down after removal, should I decrement i after removing an item so it performs the loop with the same value again? 另外,由于移除后向量中的元素向下移动,因此移除项后是否应将i减1,以使其再次以相同的值执行循环? Or is there a more elegant way of doing it? 还是有一种更优雅的方式呢?

You could try: 您可以尝试:

Entity::EntityList.erase(Entity::EntityList.begin() + i);

And yes, you should decrement i. 是的,你应该减一。

I would recommend using the Erase/Remove idiom . 我建议使用Erase / Remove习惯用法 You can supply the results of std::remove_if to vector::erase and you should be all set. 您可以将std :: remove_if的结果提供给vector :: erase ,您应该已经准备就绪 It would look something like this: 它看起来像这样:

entityList.erase(
    std::remove_if(entityList.begin(), entityList.end(), isEntityDead));

Here is an example: 这是一个例子:

#include <algorithm>
#include <vector>

class Entity {
public:
    bool isDead;
};

// Need this since isDead isn't a member function of Entity
bool isEntityDead(Entity& entity) {
    return entity.isDead;
}

int main(int argc, char** argv) {
    Entity ent1 = Entity();
    ent1.isDead = false;
    Entity ent2 = Entity();
    ent2.isDead = true;
    Entity ent3 = Entity();
    ent3.isDead = false;

    std::vector<Entity> entityList;
    entityList.push_back(ent1);
    entityList.push_back(ent2);
    entityList.push_back(ent3);

    std::cout << "Before removing anything: " << entityList.size() << std::endl;

    entityList.erase(std::remove_if(entityList.begin(), entityList.end(), isEntityDead));

    std::cout << "After remove/erase: " << entityList.size() << std::endl;

    return 0;
}

I would recommend not modifying the contents of the vector while you're looping through it if you can help it. 如果可以帮助,建议您在遍历向量时不要修改向量的内容。 If you need to remove the dead entities at the end of your game loop then you're better off figuring out which entities are "dead", putting them in a "dead list" and then removing anything in the "dead list" from your entity list all at once. 如果您需要在游戏循环结束时删除死掉的实体,那么最好确定哪些实体是“死掉的”,将它们放入“死掉的列表”,然后从您的游戏中删除“死掉的列表”中的所有内容实体列表一次。

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

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