【发布时间】:2011-12-24 13:01:22
【问题描述】:
我想从容器中删除当前在基于范围的 for 循环中使用的元素。这会导致未定义的行为吗?或者如果我不调用erase(),erase() 之后的下一个值 element 会是下一个元素应该是什么?
例子:
std::map<int, int> someMap;
/* Fill in someMap */
for (auto& element : someMap)
{
/* ... */
if ( /* Some condition */ )
someMap.erase(element.first);
}
【问题讨论】: