C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 从std :: multimap <>删除项目后,我可以继续使用迭代器吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > What happens if you call erase() on a map element while iterating from begin to end?@R_607_5179@@R_607_5179@@R_607_5179@@R_607_5179@@R_607_5179@ 3个
即使在@L_928_1@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

@H_879_8@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.
    }
}

另见:
What happens if you call erase() on a map element while iterating from begin to end?

delete a specific entry in the map,but the iterator must point to the next element after the deletion

大佬总结

以上是大佬教程为你收集整理的c – 从std :: multimap <>删除项目后,我可以继续使用迭代器吗?全部内容,希望文章能够帮你解决c – 从std :: multimap <>删除项目后,我可以继续使用迭代器吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。