c++(谓词)
c++(谓词)
谓词
class findNum
{
public:
//一元谓词 谓词体现在返回值时bool 一元体现在 形参上
bool operator()(int num)
{
return num > 20;
}
};
//一元谓词
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//查找第一个大于20的数字
//第三个参数 函数对象 or 匿名对象
auto pos = find_if(v.begin(), v.end(), findNum());
if (pos != v.end())
cout << "找到了大于20的数字: " << *pos << endl;
else
cout << "没有找到" << endl;
}
浙公网安备 33010602011771号