Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在迭代误差中间将对象添加到向量中 所以,我处于一个循环中,迭代向量中的所有对象: for (auto &itr : m_entities) { itr.second->Update(l_time); if (itr.second->m_isDead) Remove(itr.first); //don't worry, it does not remove immediately so the vector size wont change }_C++_Loops_Vector_Containers_Sfml - Fatal编程技术网

C++ 在迭代误差中间将对象添加到向量中 所以,我处于一个循环中,迭代向量中的所有对象: for (auto &itr : m_entities) { itr.second->Update(l_time); if (itr.second->m_isDead) Remove(itr.first); //don't worry, it does not remove immediately so the vector size wont change }

C++ 在迭代误差中间将对象添加到向量中 所以,我处于一个循环中,迭代向量中的所有对象: for (auto &itr : m_entities) { itr.second->Update(l_time); if (itr.second->m_isDead) Remove(itr.first); //don't worry, it does not remove immediately so the vector size wont change },c++,loops,vector,containers,sfml,C++,Loops,Vector,Containers,Sfml,m_entities是一个处理从基类继承的派生对象的向量,循环只调用该向量中每个对象的Update()函数。很简单,一切正常 但是,当我决定调用Add()函数,该函数只向向量添加一个新对象时,我得到了一个异常: _Myval2是0xDDDD 我知道问题所在:我在一个派生类中调用Update()函数中的Add()函数,该函数在仍然进行迭代的同时更改向量的大小。像这样: for (auto &itr : m_entities) { itr.second->Update(l_ti

m_entities
是一个处理从基类继承的派生对象的向量,循环只调用该向量中每个对象的
Update()
函数。很简单,一切正常

但是,当我决定调用
Add()
函数,该函数只向向量添加一个新对象时,我得到了一个异常:

_Myval2是0xDDDD

我知道问题所在:我在一个派生类中调用
Update()
函数中的
Add()
函数,该函数在仍然进行迭代的同时更改向量的大小。像这样:

for (auto &itr : m_entities) {
    itr.second->Update(l_time); //here is where I call "Add," changing the size of the vector  
    if (itr.second->m_isDead)   //and messing everything up
        Remove(itr.first);
}
所以我的问题是:如何在循环中添加向量,并且仍然能够完成循环而不产生错误

PS:我需要把它添加到循环中。如果我像使用
Remove()
函数那样,那么我想要的功能就不起作用了。
Remove()
函数仅将实体的ID推送到一个向量,该向量稍后将从该向量中删除。是的,如果我使用
Add()
函数执行类似操作,它将起作用,但只要我在循环中添加实体,我就想修改它:

if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
    if (...) {
        ...

        //Here it will add to the vector and return the entity ID, which 
        //I use to find the entity and modify it right away
        unsigned int bulletId = m_entityMgr->Add(EntityType::Bullet);

        //just a pointer to the newly added entity so I can modify it
        Bullet *bullet = (Bullet*)m_entityMgr->Find(bulletId);

        ... //modifying entity
    }
}

然后在它退出这个
Update()
函数之后,它返回到那个循环,错误弹出,因为向量的大小被修改了。

在基于范围的
for
循环中,你不能做任何修改容器大小的事情。在这种情况下,由于只添加元素,因此只需使用传统的基于索引的
for
循环即可,例如:

for(size_t i=0;iUpdate(l_time);//可能会增加向量的大小
如果(itr.second->m_isDead)
移除(itr.优先);
}
或者,如果您不想对添加的新项目调用
Update()

size\u t size=m_entities.size();
对于(大小i=0;iUpdate(l_time);//可能会增加向量的大小
如果(itr.second->m_isDead)
移除(itr.优先);
}

在使用基于范围的
for
时,无法更改容器中元素的数量。你知道它是用来记账的。有些容器比其他容器更宽容,但这是
vector
不友好的一种情况。有没有一种解决方案可以在不使其失效的情况下解决此问题?您不能只存储那些由
Add/Find
生成的指针,然后再对其进行处理(就像您对
Remove
所做的那样)?Remove code上的战术说明:。你应该能让它为你工作。谢谢你,成功了。