vector的erase函数
2014-01-18 17:07
288 查看
vector的erase函数,记住该函数会使迭代器失效,返回下一个迭代器。
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> iVec;
vector<int>::iterator it;
int i;
for( i=0;i<10;i++)
iVec.push_back(i);
for( i=0;i<iVec.size();i++)
cout<<iVec[i]<<ends;
cout<<endl;
for(it=iVec.begin();it!=iVec.end();++it)
{
int h=*it;
if(h ==4 || h == 5)
{
it=iVec.erase(it);//此时it指向5
it--; //这里--是有用意的,否则删不了5的
}
}
for( i=0;i<iVec.size();i++)
cout<<iVec[i]<<ends;
cout<<endl;
system("pause");
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> iVec;
vector<int>::iterator it;
int i;
for( i=0;i<10;i++)
iVec.push_back(i);
for( i=0;i<iVec.size();i++)
cout<<iVec[i]<<ends;
cout<<endl;
for(it=iVec.begin();it!=iVec.end();++it)
{
int h=*it;
if(h ==4 || h == 5)
{
it=iVec.erase(it);//此时it指向5
it--; //这里--是有用意的,否则删不了5的
}
}
for( i=0;i<iVec.size();i++)
cout<<iVec[i]<<ends;
cout<<endl;
system("pause");
return 0;
}
相关文章推荐
- UVA 10382喷水设施
- 赫夫曼编码
- 【OpenGL4.0】GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形
- Fifa Ultimate Team Millionaire Trading Center - Launching Now!
- Yii Framework 开发教程(14) UI 组件 MaskedTextField示例
- sdut2164Binomial Coeffcients(组合数求模)
- Mac和Windows中常见中文字体的英文名称
- Cordova 3.3 开发环境搭建(视频)
- java学习笔记(一)
- XML
- Yii Framework 开发教程(13) UI 组件 ContentDecorator示例
- Yii Framework 开发教程(12) UI 组件 ClipWidget示例
- 百度开源编辑器内容获取与更改
- Linux命令详解之find
- Yii Framework 开发教程(11) UI 组件 ActiveForm示例
- LA2965字符串合并
- js取float型小数点后两位数的方法
- 多路查找树之2-3树的删除原理 - 数据结构和算法81
- UVALive2678子序列
- openerp修改logo和title