vector list map 遍歷刪除制定元素 防止迭代器失效的實(shí)例
方法如下所示:
// k_control.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。
//
#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;
}
以上就是小編為大家?guī)淼膙ector list map 遍歷刪除制定元素 防止迭代器失效的實(shí)例全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(152.求最大子數(shù)組乘積)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(152.求最大子數(shù)組乘積),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼
本文主要介紹了Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼,管理員針對不同人員進(jìn)行權(quán)限設(shè)定,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)代碼
本篇文章是對C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05QT獲取顯示當(dāng)前時(shí)間和日期的方法(用QTime,QDate和QDateTime)
獲取當(dāng)期日期時(shí)間在我們?nèi)粘i_發(fā)中經(jīng)常會遇到,下面這篇文章主要給大家介紹了關(guān)于QT獲取顯示當(dāng)前時(shí)間和日期的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08