vector list map 遍历删除制定元素 防止迭代器失效
2016-02-28 11:02
429 查看
// k_control.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include <vector>
#include <map>
#include <string>
#include <list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
printf("run main");
vector<int> vect ;
vect.push_back(1);
vect.push_back(2);
vect.push_back(3);
vect.push_back(4);
vect.push_back(5);
vector<int>::iterator iter = vect.begin();
for(iter;iter!=vect.end();){
if(*iter == 3){
iter=vect.erase(iter);
}else{
iter++;
}
}
map<int,string> map_local ;
map_local[1]="hello_1";
map_local[2]="hello_2";
map_local[3]="hello_3";
map_local[4]="hello_4";
map_local[5]="hello_5";
map<int,string>::iterator iter_map=map_local.begin();
for(iter_map;iter_map!=map_local.end();){
if(iter_map->first==1){
map_local.erase(iter_map++);
或者
//iter_map=map_local.erase(iter_map);
}else{
iter_map++;
}
}
list<int> list_my;
list_my.push_back(1);
list_my.push_back(2);
list_my.push_back(3);
list_my.push_back(4);
list_my.push_back(5);
list<int>::iterator iter_list = list_my.begin();
for(iter_list;iter_list!=list_my.end();){
if(*iter_list==2){
list_my.erase(iter_list++);
或者
//iter_list=list_my.erase(iter_list);
}else
iter_list++;
}
printf("run over");
return 0;
}
//
#include "stdafx.h"
#include "stdio.h"
#include <vector>
#include <map>
#include <string>
#include <list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
printf("run main");
vector<int> vect ;
vect.push_back(1);
vect.push_back(2);
vect.push_back(3);
vect.push_back(4);
vect.push_back(5);
vector<int>::iterator iter = vect.begin();
for(iter;iter!=vect.end();){
if(*iter == 3){
iter=vect.erase(iter);
}else{
iter++;
}
}
map<int,string> map_local ;
map_local[1]="hello_1";
map_local[2]="hello_2";
map_local[3]="hello_3";
map_local[4]="hello_4";
map_local[5]="hello_5";
map<int,string>::iterator iter_map=map_local.begin();
for(iter_map;iter_map!=map_local.end();){
if(iter_map->first==1){
map_local.erase(iter_map++);
或者
//iter_map=map_local.erase(iter_map);
}else{
iter_map++;
}
}
list<int> list_my;
list_my.push_back(1);
list_my.push_back(2);
list_my.push_back(3);
list_my.push_back(4);
list_my.push_back(5);
list<int>::iterator iter_list = list_my.begin();
for(iter_list;iter_list!=list_my.end();){
if(*iter_list==2){
list_my.erase(iter_list++);
或者
//iter_list=list_my.erase(iter_list);
}else
iter_list++;
}
printf("run over");
return 0;
}
相关文章推荐
- Centos5.4+Nginx-0.8.50+UWSGI-0.9.6.2+Django-1.2.3搭建高性能WEB服务器
- html textarea标签的innerHTML属性和value属性的区别
- IO流 字符流=字节流+编码表
- IbeoSdk4.3.1使用问题【1】-- ibeosdk_VS2012.lib无法解析的外部符号
- clean code
- 4.1.3.1 Android Broadcast-Receiver的心得
- 2-8-双循环链表链式存储结构-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- c++ gui qt4 chap01 age
- [iOS]Apple Pay接入详细教程
- 数据结构:索引之线性索引
- Error Unable to start the Genymotion virtual device.解决
- 当梦想照进现实
- 当前端也拥有 Server 的能力
- 【leetcode】【18】4Sum
- 使用IIS发布网址之后,访问时需要用户名和密码。
- 更多Eclipse操作快捷键
- 【leetcode】Array——Remove Duplicates from Sorted Array II(80)
- 活着
- NodeJS的代码调试和性能调优
- 调查问卷