//使迭代器失效,注意!!!!
typedef std::map<std::string,float> StringFloatMap;
StringFloatMap coll;
StringFloatMap::iterator pos;
...
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
if (pos->second == value) {
coll. erase (pos); // 出错 !!!
}
}
//正确处理迭代器所指元素的方法
typedef std::map<std::string,float> StringFloatMap;
StringFloatMap coll;
StringFloatMap::iterator pos, tmp_pos;
...
//remove all elements having a certain value
for (pos = c.begin(); pos != c.end(); )
{
if (pos->second == value) {
c.erase(pos++); // Make clear!!
}
else {
++pos;
}
}