Can you give some example or a link to a topic.
Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value.
The STL provides 3 algorithms that indicates whether all, some, or none of the elements of a range satisfy a given condition. The condition is itself expressed by a predicate, that is to say a function pointer (or object) that take an element of the range and returns a bool .
A predicate asks a question where the answer is true or false or, said another way, yes or no. In computer science and in mathematics, this question comes in the form of a function. The result of the function is true or false (yes or no).
A predicate is an expression of one or more variables defined on some specific domain. A predicate with variables can be made a proposition by either assigning a value to the variable or by quantifying the variable. The following are some examples of predicates − Let E(x, y) denote "x = y"
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:
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).
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 objectpred
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:
if
statement (and hence because of C++'s language rules, also in a while
loop and so on).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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With