【发布时间】:2012-05-18 05:51:34
【问题描述】:
可能重复:
How to filter items from a std::map?
What happens if you call erase() on a map element while iterating from begin to end?
我有对象映射,我想遍历它并删除一些条目。
typedef std::map<A,B> MapT;
MapT m;
MapT::iterator it;
for(it = m.begin(); it != m.end(); it++ ) {
if( condition ) m.erase(it);
}
这样可以吗?
【问题讨论】:
-
一旦修改地图,迭代器将失效。
-
见here。 (重复)