I was under the impression one cant use erase
on a const iterator
. Check this code.
Why does the below code compile (C++11, gcc)?
long getMax(const bool get_new)
{
long max_val=0;
TO now=getNow();
map<TO, long>& m=get_new?m_new:m_old;
for(auto it=m.cbegin(); it !=m.cend())
{
if(now.compareTime((*it).first)<lookback)
{
max_val=max(max_val,
(*it).second);
++it;
}
else
{
it=m.erase(it);
}
}
return max_val;
}
The map itself is not constant, but my understanding is that the const iterator
should make this fail.
The behavior has changed from C++11; std::map::erase takes
const_iterator
as its parameter.For
std::map::erase
, the passed iterator is just used as the position where the element would be deleted, not for modifying the element through it. That meansconst_iterator
would be fine. Before C++11, the support forconst_iterator
was not very good, but the situation has changed from C++11. You should useconst_iterator
instead ofiterator
when possible now.