源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

vector list map 遍历删除制定元素 防止迭代器失效的实例

  • 时间:2021-07-02 00:46 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:vector list map 遍历删除制定元素 防止迭代器失效的实例
[b]方法如下所示:[/b] // 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){ [b]iter=vect.erase(iter);[/b] }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++); 或者 [b]//[/b][b]iter_map=map_local.erase(iter_map);[/b] }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++); 或者 //[b]iter_list=list_my.erase(iter_list); [/b]}else iter_list++; } printf("run over"); return 0; } 以上就是小编为大家带来的vector list map 遍历删除制定元素 防止迭代器失效的实例全部内容了,希望大家多多支持编程素材网~
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部