删除元素时,地图迭代器如何无效? [英] how are map iterators invalidated when erasing elements?

查看:97
本文介绍了删除元素时,地图迭代器如何无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用擦除方法时,何时以及如何使映射中的迭代器无效?

when and how are iterators invalidated in a map when using the erase method ?

例如:

std :: map < int , int > aMap ;

aMap [ 33 ] = 1 ;
aMap [ 42 ] = 10000 ;
aMap [ 69 ] = 100 ;
aMap [ 666 ] = -1 ;

std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ;

for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ;
      it != itEnd ;
      // no-op
    )
{
   aMap.erase ( it ++ ) ;
}

已擦除的迭代器肯定会变得无效(在仍然有效时会递增) 但是其他人呢?

the erased iterator will surely become invalid (it's incremented while still valid) but what about the others?

如果我没记错的话,标准会说映射必须是平衡的二叉树或具有等效键搜索复杂性的结构

if I'm not wrong the standard says that a map has to be a balanced binary tree or a structure with equivalent key-search complexity

如果地图是用树实现的,我可以假定未擦除的迭代器仍然有效吗?

in case the map is implemented with a tree, can I assume that not erased iterators remain valid ?

实现地图的其他可能方式又如何呢?

what about other possible ways to implement a map ?

推荐答案

仅擦除的迭代器无效,其余标准保证其余部分仍然有效.

Only the erased iterator is invalid, the rest are guaranteed by the standard to remain valid.

请参见迭代器无效规则

这篇关于删除元素时,地图迭代器如何无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆