What is predicate in C++? [closed]

廉价感情. 提交于 2019-12-17 17:39:51

问题


Can you give some example or a link to a topic.


回答1:


A predicate is a C++ function returning a boolean or an object having a bool operator() member. A unary predicate takes one argument, a binary takes two, and so on. Examples of questions predicates can answer for a particular algorithm are:

  • Is this element what we are looking for?
  • Is the first of two arguments ordered first in our order?
  • Are the two arguments equal?

Almost all STL algorithms take a predicate as last argument.

You can construct new predicates using standard, self-defined, and/or predicate-making classes (here is a good reference).




回答2:


The C++ standard defines Predicate as follows (25/7):

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.

There's an analogous definition of BinaryPredicate with two parameters.

So in English, it's a function or an object with a operator() overload, that:

  • takes a single parameter. In the case of algorithms, the parameter type is implicitly convertible from the type of the dereferenced iterator of the algorithm in question, or is a const reference to such a type, or at a push it can be a non-const reference to the exact type as long as the iterator isn't a const_iterator.
  • returns a value that can be tested for truth in an if statement (and hence because of C++'s language rules, also in a while loop and so on).
  • doesn't modify its arguments (at least, not as long as the parameter type is const-correct...)

Additionally, since many algorithms don't specify the exact order of operations they perform, you might find that you get unpredictable behavior if your predicate isn't consistent, i.e. if the result depends on anything other than the input value that can change between calls.

As well as algorithms, the logical negator not1 in <functional> takes a Predicate template parameter. In that case, there's an extra requirement (20.3/5):

To enable adaptors and other components to manipulate function objects that take one or two arguments it is required that the function objects correspondingly provide typedefs argument_type and result_type for function objects that take one argument and first_argument_type, second_argument_type, and result_type for function objects that take two arguments.




回答3:


It is not specific to C++ (or even computer languages). In natural language grammar, in a statement such as the gate is open, the is open part is the predicate and is either true or false, so say you had a class cGate, with a member function bool cGate::isOpen(), such a function would be a predicate.

Essentially if the function asks a question about the object state or value and the result is either true or false, then it is a predicate.




回答4:


A predicate is simply a function that returns true or false depending on whether its input(s) satisfy some condition. In general, a predicate function should be pure; it should always return the same result when given the same input (so bool isDateInPast(Date &date) would be a bad predicate).

They are often used, for example, as callbacks for STL sorting routines (i.e. "is input a less than input b?").



来源:https://stackoverflow.com/questions/5921609/what-is-predicate-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!