通过迭代器删除容器中的元素

为防止迭代器失效,采用以下方法来避免。

	for (pos = coll.begin(); pos != coll.end();)
	{
		if (pos->second == value)
		{
			coll.erase(pos++);
		}
		else
		{
			++pos;
		}
	}

  

map<string, ShapeFactory*>::iterator it = ShapeFactory::factories.begin();
        
        while(it != ShapeFactory::factories.end())
            delete it++->second;

 

posted @ 2013-09-09 10:38  calabashdad  阅读(222)  评论(0编辑  收藏  举报