错误写法:

map<int, int> m;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
m.erase(it);
}

这样会导致程序行为不可知。因为map是关联容器,对于关联容器来说,如果某一个元素已经被删除,那么其对应的迭代器就失效了,不应该再被使用;否则会导致程序无定义的行为。

正确写法1(STL推荐写法):

map<int, int> m;
for (map<int, int>::iterator it = m.begin(); it != m.end(); )
{
m.erase(it++);
}

使用删除之前的迭代器定位下一个元素。逻辑上来说是在for里做++和m.erase(it++)是一样的,但可能是与编译器的编译方式有关。编译器可以识别erase(it++)这样的操作并做相应优化避免不可知行为。

正确写法2:

map<int, int> m;
for (map<int, int>::iterator it = m.begin(); it != m.end(); )
{
it = m.erase(it);
}

erase()函数返回下一个元素的迭代器。

map erase iterator的更多相关文章

  1. map::erase陷阱

    map::erase函数在不同版本stl中的差异 1. C++98和C++11标准 http://www.cplusplus.com/reference/map/map/erase/ 2. pj st ...

  2. 关于map::erase的使用说明

    C++ 中经常使用的容器类有vector,list,map.其中vector和list的erase都是返回迭代器,但是map就比较不一样. 当在循环体中使用map::erase语句时,为了能够在任何机 ...

  3. [转] C++ STL中map.erase(it++)用法原理解析

    总结一下map::erase的正确用法. 首先看一下在循环中使用vector::erase时我习惯的用法: for(vector<int>::iterator it = vecInt.be ...

  4. vector.erase();vector.clear();map.erase();

    vector::erase()返回下一个iter: STL中的源码: //清除[first, last)中的所有元素 iterator erase(iterator first, iterator l ...

  5. Iterator<Entry<String,String>> iter=map.entrySet().iterator(); 是什么意思

    //获得map的迭代器,用作遍历map中的每一个键值对Iterator是迭代器,map之前应该定义过,姑且认为是HashMap.<Entry<String,String>>表示 ...

  6. map.entrySet().iterator()

    1.首先创建一个HashMap, Map map= new HashMap(); 2.Iterator iter= map.entrySet().iterator(); 首先是map.entrySet ...

  7. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  8. map set iterator not incrementable 解决办法

    例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...

  9. Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)

    首先,我们先看一下Collection集合的基本结构: 1.Collection接口 Collection是最基本集合接口,它定义了一组允许重复的对象.Collection接口派生了两个子接口Set和 ...

随机推荐

  1. Kerberos

    一.Kerberos Concept Kerberos是一种网络认证协议,其设计目标是通过密钥系统为客户机/服务器应用程序提供强大的认证服务,为通信双方提供双向身份认证. Kerberos关键术语: ...

  2. [OSG][osgEarth]osgEarth例子程序简介

    1.osgearth_graticule:生成经纬线. 2.osgearth_annotation:各类标注(点.线.面.模型.文本等). 3.osgearth_city:加载一个城市三维模型,可以浏 ...

  3. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  4. angularjs $broadcast 和 $on 的使用及其注意事项

    下面是demo: <div ng-controller="ParentCtrl"> www.111cn.net //父级 <div ng-controller=& ...

  5. The Same Game-POJ1027模拟

    The Same Game Time Limit: 1000MS Memory Limit: 10000K Description The game named "Same" is ...

  6. mysql 实现 start with

    自己写service----> 传入map(idsql,rssql,prior)   idsql 查询id   rssql 查询结果集    调用 以下方法 @param ids 要查询的起始 ...

  7. java:IO流学习小结

    可以看以下内容学习一下: http://blog.csdn.net/zzp_403184692/article/details/8057693

  8. shutdown的简单小应用

    关于shutdown的操作命令在此不做详细叙述 1.WIN+R ,输入cmd即打开cmd命令界面 2.输入shutdown /help,出现下图 这里介绍一个关于shutdown的小应用: 比如我们的 ...

  9. python 给定n,返回n以内的斐波那契数列

    方式一:函数 def fabs(n): a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b fabs(1000) 方式二:列表 re ...

  10. VMWare虚拟机设置固定ip上网方法

    转自:http://blog.csdn.net/cyberrusher/article/details/7269795 1. 在VMWare工具栏中打开:编辑--->虚拟机网络编辑器, 打开VM ...