qfedu-cpp-level/day10/stl_algorithm_demo/d2.cpp

60 lines
966 B
C++
Raw Normal View History

2023-08-14 17:20:39 +08:00
// 谓词
// gt 大于, ge 大于等于
// lt 小于, le 小于等于
// eq 等于, ne 小于等于
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class GTFive // 一元谓词
{
public:
bool operator()(int n)
{
return n > 5;
}
};
class GT // 二元谓词
{
public:
bool operator()(const int &n1, const int &n2)
{
return n1 > n2;
}
};
void printGTF(GTFive gtf, int *p, int size)
{
for (int i = 0; i < size; i++)
{
if (gtf(p[i]))
cout << p[i] << " ";
}
cout << endl;
}
void print(int *p, int size, GT gt, int n)
{
for (int i = 0; i < size; i++)
{
if (gt(p[i], n))
cout << p[i] << " ";
}
cout << endl;
}
int main()
{
int ms[] = {1, 2, 4, 7, 9, 20};
cout << "大于 5 的元素" << endl;
printGTF(GTFive(), ms, 6);
cout << "大于 7 的元素" << endl;
print(ms, 6, GT(), 7);
return 0;
}