是的,如果您删除一个迭代器,则该迭代器将获得所谓的 singular value
,这意味着它不再属于任何容器。您不能再递增,递减或读出/写入它。进行该循环的正确方法是:
for(map<T, S*>::iterator it = T2pS.begin(); it != T2pS.end(); T2pS.erase(it++)) { // wilhelmtell in the comments is right: no need to check for NULL. // delete of a NULL pointer is a no-op. if(it->second != NULL) { delete it->second; it->second = NULL; }}对于在擦除一个迭代器时可能使其他迭代器无效的容器,请
erase返回下一个有效迭代器。然后你用
it = T2pS.erase(it)
这就是
std::vectorand的工作方式
std::deque,但
std::mapor 却没有
std::set。



