一 在vector中查找元素
1 代码
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main(){
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vector<int>::iterator iter=find(vec.begin(),vec.end(),3);
if ( iter==vec.end())
cout << "Not found" << endl;
else
cout << "Found" << endl;
return 0;
}
2 运行
[root@localhost charpter03]# g++ 0312.cpp -o 0312
[root@localhost charpter03]# ./0312
Found
3 说明
使用find函数在vector中查找。注意find函数并不属于vector的成员,而存在于算法中,所以应当加上头文件#include<algorithm>
二 vecrot的删除函数说明
1 点睛
vector的删除,可以有erase或pop_back函数。erase可以删除指定元素,而pop_back只能删除向量的最后一个数据。
2 erase函数

最低0.47元/天 解锁文章
6446

被折叠的 条评论
为什么被折叠?



