C++:从映射中删除迭代器,然后递增到下一个迭代器
此方法会导致中止错误:“map/set 迭代器不可递增。”
由于 if
失败并且确定了应该删除的有效迭代器(并且确实如此),通过 ++_iter
继续到映射中的下一个迭代器失败因为 _iter
不再是有效的对象/指针。
迭代地图并能够删除整个地图中的单个项目的正确程序是什么?
typedef std::map<std::string, BITMAP*> MapStrBmp;
typedef MapStrBmp::iterator MapStrBmpIter;
\\...
void BitmapCache::CleanCache() {
//Clean the cache of any NULL bitmaps that were deleted by caller.
for(MapStrBmpIter _iter = _cache.begin(); _iter != _cache.end(); ++_iter) {
if(_iter->second != NULL) {
if((_iter->second->w < 0 && _iter->second->h < 0) == false) continue;
}
_cache.erase(_iter);
}
}
This method causes an abort error: "map/set iterator not incrementable."
Due to that after the if
fails and a vaild iterator that should be erased is determined, (and is), continuing to the next iterator in the map via ++_iter
fails because _iter
is no longer a valid object/pointer.
What is the correct procedure for iterating through a map AND having the ability to remove individual items throughout?
typedef std::map<std::string, BITMAP*> MapStrBmp;
typedef MapStrBmp::iterator MapStrBmpIter;
\\...
void BitmapCache::CleanCache() {
//Clean the cache of any NULL bitmaps that were deleted by caller.
for(MapStrBmpIter _iter = _cache.begin(); _iter != _cache.end(); ++_iter) {
if(_iter->second != NULL) {
if((_iter->second->w < 0 && _iter->second->h < 0) == false) continue;
}
_cache.erase(_iter);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你只需要更加小心一点:
You just have to be a bit more careful:
map::erase(iterator)
为您提供一个迭代器,该迭代器指向擦除后映射中的下一个元素(如果有)。因此,您可以这样做:map::erase(iterator)
gives you an iterator pointing to the next element in the map (if any) after erasing. Therefore, you can do:关联容器的标准擦除循环:
The standard erase loop for an associative container:
在迭代期间安全擦除迭代器的规范方法是使用
container::erase
的结果:The canonical way to safely erase iterators during an iteration is to use the result of
container::erase
: