I want to erase an element from a container which is being currently used within a ranged-based for loop. Will this cause undefined behaviour? Or will the next value of element
after erase()
be what the next element is supposed to be if I didn't call erase()
?
Example:
std::map<int, int> someMap;
/* Fill in someMap */
for (auto& element : someMap)
{
/* ... */
if ( /* Some condition */ )
someMap.erase(element.first);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…