c++11 - Using iterator for removing items from map in c++ -
map<int, int> m_map; // ... map::iterator = m_map.begin(); while (it != m_map.end()) { m_map.erase(it++); }
when ++
action take place? before or after erase
? when safe so?
i don't think it's specified whether ++
happens before or after call erase
. still, if guaranteed peformed before call, fact had asks shows code bad. there better, 100% safe alternative:
while (it != m_map.end()) { = m_map.erase(it); }
erase
returns iterator element past erased one.
Comments
Post a Comment