C++ 谓词(predicate) 与 仿函数 ( functor (function object))

谓词与函数对象

谓词 predicate

C++ 标准定义谓词如下:

The Predicate parameter is used whenever an algorithm expects a function object that when applied to the result of dereferencing the corresponding iterator returns a value testable as true. In other words, if an algorithm takes Predicate pred as its argument and first as its iterator argument, it should work correctly in the construct if (pred(*first)){...}. The function object pred shall not apply any non-constant function through the dereferenced iterator. This function object may be a pointer to function, or an object of a type with an appropriate function call operator.

换成中文就是:当一个算法需要一函数对象,去应用于解引用(deferencing)迭代器(deferencing 解释: int p;将定义一个指向整数的指针,并p取消引用该指针,这意味着它将实际检索p指向的数据。)的结果,谓词参数返回一个testable值为true。换句话说,换句话说, 如果算法将预测 pred 作为其参数,第一个参数是迭代参数,则如果 (pred (* first)) {..} , 它应该在构造中正常工作。函数对象 pred 不应通过解引用迭代器应用于任何非定常(non-constant)函数。此函数对象可以是指向函数的指针, 也可以是具有适当函数调用运算符的类型的对象。

仿函数

functor(函数对象或函数)形式:object + ()

猜你喜欢

转载自www.cnblogs.com/gardenofhu/p/10062378.html