c – 从std :: multimap <>删除项目后,我可以继续使用迭代
发布时间:2020-12-16 10:41:46 所属栏目:百科 来源:网络整理
导读:参见英文答案 What happens if you call erase() on a map element while iterating from begin to end?????????????????????????????????????3个 即使在调用multimap :: erase()之后,我还能继续使用多重映射迭代器吗?例如: Blah::iterator iter;for ( ite
参见英文答案 >
What happens if you call erase() on a map element while iterating from begin to end?????????????????????????????????????3个
即使在调用multimap :: erase()之后,我还能继续使用多重映射迭代器吗?例如: Blah::iterator iter; for ( iter = mm.begin(); iter != mm.end(); iter ++ ) { if ( iter->second == something ) { mm.erase( iter ); } } 是否应该正确运行,或者在调用擦除后迭代器是否无效?像http://www.cplusplus.com/reference/stl/multimap/erase.html这样的参考站点在迭代器的生命周期或者建设性/破坏性方法对迭代器的影响这个主题上是非常安静的. 解决方法
http://www.sgi.com/tech/stl/Multimap.html
Multimap has the important property that inserting a new element into a multimap does not invalidate iterators that point to existing elements. Erasing an element from a multimap also does not invalidate any iterators,except,of course,for iterators that actually point to the element that is being erased. 所以看起来应该是这样的: Blah::iterator iter; for ( iter = mm.begin();iter != mm.end();) { if ( iter->second == something ) { mm.erase( iter++ ); // Use post increment. This increments the iterator but // returns a copy of the original iterator to be used by // the erase method } else { ++iter; // Use Pre Increment for efficiency. } } 另见: 和 delete a specific entry in the map,but the iterator must point to the next element after the deletion (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |