vector中的remove_if使用:
remove_if(ivec.begin(), ivec.end(), function)
作用:删除满足function函数的元素。实例如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(int d)
{
return d < 30;
}
int main()
{
int myints[] = { 15, 36, 7, 17, 20, 39, 4, 1 };
vector<int> ivec(myints, myints + 8); // 15 36 7 17 20 39 4 1
ivec.erase(remove_if(ivec.begin(), ivec.end(), compare), ivec.end()); //删除满足compare条件的元素
for (vector<int>::iterator it = ivec.begin(); it != ivec.end(); ++it)
cout << *it << endl; //36 39
return 0;
}
vector初始化,请参考我的博客:
c++ vector(向量)使用方法详解(顺序访问vector的多种方式)
vector删除,请参考我的博客:
vector删除元素