1、需要的头文件:
#include <algorithm>
2、参数说明
remove_if的前两个参数不难理解。
第三个参数可以是返回bool的函数指针,也可以是重载=(bool operator() (…))的类对象;从remove_if的函数定义往里面跟,可以看到这样一句:if (_Pred(*_First))。
3、示例代码
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class CTest
{
public:
CTest(int nData)
{
m_nData = nData;
}
bool operator() (const CTest & test)
{
if (test.m_nData > 5)
{
return true;
}
return false;
}
~CTest(){};
//private:
int m_nData;
};
bool Com(CTest test)
{
if (test.m_nData > 5)
{
return true;
}
return false;
}
bool intCom(int nData)
{
return (nData > 5);
}
int _tmain()
{
vector<CTest> tests;
vector<int> inttest;
for (int i=10; i>0; i--)
{
CTest test(i);
tests.push_back(test);
inttest.push_back(i);
}
for (vector<CTest>::iterator it = tests.begin(); it != tests.end(); it++)
{
vector<CTest>::iterator itEnd = remove_if(tests.begin(), tests.end(), CTest((*it).m_nData));
tests.erase(itEnd, tests.end());
}
for (vector<int>::iterator it = inttest.begin(); it != inttest.end(); it++)
{
vector<int>::iterator itEnd = remove_if(inttest.begin(),inttest.end(), intCom);
inttest.erase(itEnd, inttest.end());
}
system("pause");
return 0;
}