C++ vector遍历删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ vector遍历删除相关的知识,希望对你有一定的参考价值。
参考技术A int _tmain(int argc, _TCHAR* argv[])vector<int> a ;//= 12, 23, 34, 45, 56, 67, 78, 89;
a.push_back(10);
a.push_back(40);
a.push_back(10);
a.push_back(10);
a.push_back(10);
for (auto iter = a.begin(); iter != a.end(); )
if (*iter > 30)
iter = a.erase(iter);
else
++iter;
感谢 C海摸鱼 的指正
N种姿势满足你遍历C++ vectorC++ vector 遍历
C++ vector遍历demo,多种姿势任你选~
1.迭代器
for(vector<int>::iterator it = obj.begin(); it != obj.end(); it++)
{
cout << *it << ",";
}
2.C++11 新增关键字auto
for(auto it = obj.begin(); it != obj.end(); it++)
{
cout << *it << ",";
}
3.数组遍历法
for(int i=0;i<obj.size();i++)
{
cout<<obj[i]<<",";
}
4.基于范围的for循环
for (auto iter : obj)
{
cout << iter << ",";
}
demo示例
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int>obj;
for(int i=0;i<10;i++)
{
obj.push_back(i);
cout<<obj[i]<<",";
}
cout << endl << "vector size is :" << obj.size() << endl;
//iterator遍历
for(vector<int>::iterator it = obj.begin(); it != obj.end(); it++)
{
cout << *it << ",";
}
cout << endl;
//数组循环遍历
for(int i=0;i<obj.size();i++)
{
cout<<obj[i]<<",";
}
cout << endl;
//auto关键字遍历遍历
for(auto it = obj.begin(); it != obj.end(); it++)
{
cout << *it << ",";
}
cout << endl;
//基于范围的for循环
for (auto iter : obj)
{
cout << iter << ",";
}
cout << endl;
return 0;
}
结果展示:
结束寄语
有关vector小细节的介绍就到此结束啦!下篇博文与各位再见面~
写博不易,如蒙厚爱,赏个关注,一键三连~~点赞+评论+收藏🤞🤞🤞,感谢您的支持~~
以上是关于C++ vector遍历删除的主要内容,如果未能解决你的问题,请参考以下文章