国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

vector list map 遍歷刪除制定元素 防止迭代器失效的實例

2020-05-23 13:56:47
字體:
來源:轉載
供稿:網友

方法如下所示:

// 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;
}

以上就是小編為大家帶來的vector list map 遍歷刪除制定元素 防止迭代器失效的實例全部內容了,希望大家多多支持VEVB武林網~


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 津南区| 灵寿县| 蓬溪县| 竹山县| 雅安市| 赫章县| 新竹市| 玉环县| 海城市| 滨州市| 曲周县| 高阳县| 惠水县| 长宁区| 四子王旗| 四川省| 万荣县| 武宣县| 资中县| 泰安市| 淄博市| 灵丘县| 盈江县| 丹凤县| 涞水县| 东平县| 木里| 古浪县| 息烽县| 金川县| 五莲县| 江阴市| 南涧| 广河县| 白水县| 新和县| 宜城市| 清徐县| 迭部县| 长治市| 乌海市|