<分区>
我有一组要迭代的对象,但是我可能会在迭代过程中决定现在需要删除其中一个(或多个)对象。
我的代码如下:
if( ! m_Container.empty() )
{
for( typedefedcontainer::iterator it = m_Container.begin();
it != m_Container.end();
++it )
{
if( ! ( SomeFunction( (*it), "test", "TEST!", false )) )
{
// If function returns false, delete object.
m_Container.erase( it );
AsyncResponseStore::iterator it = m_asyncResponses.begin();
}
}
}
当然,当我删除一个对象时,我得到一个错误:“Map/set iterator not incrementable”。有人可以建议更好的方法吗?
参见: What happens if you call erase() on a map element while iterating from begin to end?