简体   繁体   English

vector :: empty()错误,还是我错过了什么?

[英]vector::empty() bugged, or am I missing something?

I've got this piece of code, which throws an out-of-range error. 我有这段代码,它引发了超出范围的错误。

if (!_enemyVec.empty())
    {
        for (std::vector<EnemyShip*>::size_type i = 0; i != _enemyVec.size(); i++)
        {
            if (!_projVec.empty())
            {
                for (std::vector<Projectile*>::size_type y = 0; y != _projVec.size(); y++)
                {
                    ===>_projVec[y]->CheckCollision(_enemyVec[i]);
                    if (_projVec[y]->Destroy())
                        DestroyProj(y);
                    if (_enemyVec[i]->Destroy())
                        DestroyShip(i);
                }
            }
        }
    }

Notice how I've got if (!_projVec.empty()) which should be "false" if the vector _projVec is empty. 请注意,如果向量_projVec为空,我如何获得if (!_projVec.empty())应该为“ false”的信息。

The "===>" is where I get my out-of-range error(since _projVec is empty). “ ===>”是我超出范围的错误的地方(因为_projVec为空)。

According to http://www.cplusplus.com/reference/vector/vector/empty/ vector::empty "Returns whether the vector is empty (ie whether its size is 0)." “根据http://www.cplusplus.com/reference/vector/vector/empty/ vector :: empty”返回向量是否为空(即其大小是否为0)。

However when I run the code it goes into the 2nd for-loop throwing an out-of-range error, while my vector is empty (it's size is 0)? 但是,当我运行代码时,它进入了第二个for循环,引发了超出范围的错误,而我的向量为空(其大小为0)?

+       _projVec    { size=0 }  std::vector<Projectile *,std::allocator<Projectile *> >

I think I'm missing something so I'm wondering if anyone here can clear this up? 我想我缺少了一些东西,所以我想知道这里是否有人可以解决这个问题?

EDIT: Here are my DestroyProj/Ship functions. 编辑:这是我的DestroyProj / Ship函数。

void ObjectManager::DestroyShip(std::vector<EnemyShip*>::size_type &index)
{
    delete _enemyVec[index];
    _enemyVec[index] = nullptr;
    _enemyVec.erase(_enemyVec.begin() + index);
    index--;
    AddScore(10);
}

void ObjectManager::DestroyProj(std::vector<Projectile*>::size_type &index)
{
    delete _projVec[index];
    _projVec[index] = nullptr;
    _projVec.erase(_projVec.begin() + index);
    index--;
}

As BWG pointed out, it shouldn't iterate if they are empty from the beginning, so the problem probably lies where I set the index back once. 正如BWG所指出的,如果它们从一开始就为空,则不应进行迭代,因此问题可能出在我将索引重新设置一次的地方。 I also realise that this is probably a bad way to iterate through the vectors, so if someone can suggest me a different way to do it would be very much appreciated. 我还意识到,这可能是遍历向量的一种坏方法,因此,如果有人可以建议我以其他方式进行操作,将不胜感激。

Please note that during this nested for-loop I want to be able to remove an item from both vectors. 请注意,在这个嵌套的for循环中,我希望能够从两个向量中删除一个项目。

You have two nested loops modifying two vectors. 您有两个修改两个向量的嵌套循环。 While erasing from the inner vector is fine, erasing an element from the outer vector, is not (assume the outer has one element, only) 虽然从内部向量中擦除很好,但是从外部向量中擦除元素不是(假设外部只有一个元素)

Probably DestroyProj() (or one of the other functions) modifies _projVec . 大概DestroyProj() (或其他函数之一)修改_projVec So even if it wasn't empty at the start of the loop, it might become empty when projects are removed. 因此,即使在循环开始时它不为空,但在删除项目时它也可能变为空。

You should iterate over a copy of the vectors, or you should remember which indexes to remove and then remove them at the end. 您应该遍历向量的副本,或者应该记住要删除的索引,然后在最后删除它们。 Don't let DestroyProj and DestroyShip modify the vector. 不要让DestroyProjDestroyShip修改向量。

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

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