From std::map::erase()
:
References and iterators to the erased elements are invalidated. Other references and iterators are not affected.
If erase(it)
is called then it
is invalidated, and it is then used by the for
loop causing undefined behaviour. Store the return value of erase()
, which returns an iterator to the next element after the erased element (since c++11), and only increment if erase()
was not called:
for(it = nw_tot1.begin(); it != nw_tot1.end();)
{
float f=(float) ((float) it->second/lines)*100.0;
if ( f < nw_cut ) it = nw_tot1.erase(it);
else ++it;
}
In c++03 (and c++11 also) this could be accomplished with:
for(it = nw_tot1.begin(); it != nw_tot1.end();)
{
float f=(float) ((float) it->second/lines)*100.0;
if ( f < nw_cut ) nw_tot1.erase(it++);
else ++it;
}