C++中的谓词是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5921609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:11:51  来源:igfitidea点击:

What is predicate in C++?

c++classstructurepredicate

提问by munish

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

你能举一些例子或一个主题的链接。

回答by Andrey Sidorov

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:

谓词是返回布尔值或具有bool operator()成员的对象的 C++ 函数。一元谓词采用一个参数,二元谓词采用两个参数,依此类推。谓词可以为特定算法回答的问题示例如下:

  • 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 algorithmstake a predicate as last argument.

几乎所有STL 算法都将谓词作为最后一个参数。

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

您可以使用标准的、自定义的和/或谓词制作类构造新的谓词(这里是一个很好的参考)。

回答by Steve Jessop

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

C++ 标准定义Predicate如下 (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 predshall 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.

每当算法需要一个函数对象时,就会使用 Predicate 参数,该函数对象在应用于取消引用相应迭代器的结果时返回可测试为 true 的值。换句话说,如果一个算法将 Predicate pred 作为它的参数并首先作为它的迭代器参数,它应该在构造中正确工作if (pred(*first)){...}。函数对象pred不应通过解引用的迭代器应用任何非常量函数。这个函数对象可以是一个指向函数的指针,也可以是一个具有适当函数调用运算符的类型的对象。

There's an analogous definition of BinaryPredicatewith two parameters.

有一个类似的定义,BinaryPredicate带有两个参数。

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

所以在英语中,它是一个函数或一个operator()重载的对象,即:

  • 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 ifstatement (and hence because of C++'s language rules, also in a whileloop and so on).
  • doesn't modify its arguments (at least, not as long as the parameter type is const-correct...)
  • 接受单个参数。在算法的情况下,参数类型可以从所讨论算法的解除引用迭代器的类型隐式转换,或者是对这种类型的常量引用,或者在推送时它可以是对确切的非常量引用只要迭代器不是 const_iterator 就输入。
  • 返回一个可以在if语句中测试为真的值(因此,由于 C++ 的语言规则,也在while循环等中)。
  • 不修改其参数(至少,只要参数类型是常量正确的...)

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 not1in <functional>takes a Predicatetemplate parameter. In that case, there's an extra requirement (20.3/5):

与算法一样,逻辑否定符not1in<functional>接受Predicate模板参数。在这种情况下,有一个额外的要求 (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.

为了使适配器和其他组件能够操作带有一个或两个参数的函数对象,需要函数对象相应地为带有一个参数的函数对象提供 typedefargument_type 和 result_type,为带有两个参数的函数对象提供 first_argument_type、second_argument_type 和 result_type .

回答by Clifford

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 openpart 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.

它并非特定于 C++(甚至计算机语言)。在自然语言语法中,在诸如 gate is open 之类的语句中,is open部分是谓词,它要么是真要么是假,所以假设你有一个类cGate,有一个成员函数bool cGate::isOpen(),这样的函数就是一个谓词。

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.

本质上,如果函数询问有关对象状态或值的问题并且结果为真或假,则它是谓词。

回答by Oliver Charlesworth

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 alwaysreturn the same result when given the same input (so bool isDateInPast(Date &date)would be a badpredicate).

谓词只是一个函数,它根据其输入是否满足某些条件返回真或假。一般来说,谓词函数应该是纯的;当给定相同的输入时,它应该总是返回相同的结果(所以bool isDateInPast(Date &date)这将是一个糟糕的谓词)。

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

例如,它们经常用作 STL 排序例程的回调(即“输入a小于输入b”)。