前文中的程序有另一种写法:
#include <iostream>
#include <vector>
using namespace std;
template<class in, class out, in comp>
class LessThan : public unary_function<in, out>
{
public:
LessThan(){}
out operator()(in value)
{
return value < comp;
}
};
int main()
{
int array[5] = {2, 3, 1, 8, 10};
vector<int> v(array, array + 5);
int n = count_if(v.begin(), v.end(), LessThan<int, bool, 9>());
cout << n <<endl;
return 0;
}
上述程序中,将要比较对象放在模板参数中, 而不是函数参数。