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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…