bool operator()关于C++的模版,请问

来源:百度知道 编辑:UC知道 时间:2024/06/28 20:02:34
template<class T>
struct greater : public binary_function<T, T, bool> {
bool operator()(const T& x, const T& y) const;
};
那个operator定义了什么运算符?
这个具体用起来像什么样子呢?
面向对象的谓词就是functor是什么意思?

functor就是function like的类实现。那个operator声明了一个圆括号的重载。

用法:

int a[] = {0,1,2,3,4,5,6,7,8,9};

cout << *find_if(a, a + sizeof a/sizeof a[0], bind2nd(greater<int>(), 5));

在find_if函数里它会调用greater<int>()的圆括号重载,bind2nd的作用就是把5绑定为greater<int>()()的第二个参数,如:greater<int>()(x, 5),其中x是数组的每个元素。