map删除元素时防止迭代器失效的例子
2016-02-25 09:43
316 查看
很好的例子,供大家参考。
#include <map> #include <iostream> #include <stdio.h> #include <string> #include <map> using namespace std; int main() { map<int,int> _map; for (int i = 0; i < 10; i++) { _map.insert(make_pair(i,i+1)); } for (map<int,int>::iterator iter = _map.begin(); iter != _map.end();) { if (iter->first % 3 == 0) { _map.erase(iter++); } else { iter++; } } for (map<int,int>::iterator iter = _map.begin(); iter != _map.end(); ++iter) { cout<<iter->first<<endl; } return 0; }
相关文章推荐
- Hadoop2.X学习笔记--搭建
- STM32中断向量表偏移量0x200详解
- C++ 中使用json
- VC/MFC如何设置对话框背景颜色
- Windows PowerShell中scp命令
- [FastDFS] tracker报错, ERROR - file: tracker_nio.c, line: 214, recv timeout
- 归档日志
- linux-shell-#@!...
- 尝试在PYTHON中调用StanfordNLP,具体代码如下
- Python压缩多个属于不同驱动器的文件夹至同一个zip文件
- 【设计模式】策略模式
- Linux下执行可执行文件
- PATB1020
- 随机数生成算法,每一次生成都不一样
- Markdown 常用语法简单学习
- 单机数据库调整redo log日志大小
- iOS内存管理之:引用计数、ARC、自动释放池autoreleasepool和便捷方法之间的关系
- 【翻译自mos文章】改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法
- socket的accept函数解析以及服务器和多个客户端的端口问题
- JUnit常用断言及注解