小太郎*_*小太郎 10 c++ for-loop c++11
我想从当前在基于范围的for循环中使用的容器中擦除元素.这会导致未定义的行为吗?或者,如果我没有调用,那么elementafter 的下一个值是erase()下一个元素应该是什么erase()?
例:
std::map<int, int> someMap;
/* Fill in someMap */
for (auto& element : someMap)
{
/* ... */
if ( /* Some condition */ )
someMap.erase(element.first);
}
Run Code Online (Sandbox Code Playgroud)
小智 12
它应该是一个未定义的行为.因为根据14882/2011,基于范围的for语句相当于:
auto && __range = range-init;
for ( auto __begin = begin-expr(__range),
__end = end-expr(__range);
__begin != __end;
++__begin ) {
for-range-declaration = *__begin;
statement
}
Run Code Online (Sandbox Code Playgroud)