Binary Predicate: A Binary Function that returns a bool.
Strict Weak Ordering: A binary predicate that allows for a more general interpretation of "equality." Some of the standard containers consider two elements equivalent if neither is less than the other (using operator<( )). This is important when comparing floating-point values, and objects of other types where operator==( ) is unreliable or unavailable. This notion also applies if you want to sort a sequence of data records (structs) on a subset of the struct’s fields, that comparison scheme is considered a strict weak ordering because two records with equal keys are not really "equal" as total objects, but they are equal as far as the comparison you’re using is concerned. The importance of this concept will become clearer in the next chapter.
In addition, certain algorithms make assumptions about the operations available for the types of objects they process. We will use the following terms to indicate these assumptions:.
LessThanComparable: A class that has a less-than operator<.
Assignable: A class that has a copy-assignment operator= for its own type.
EqualityComparable: A class that has an equivalence operator== for its own type.
We will use these terms later in this chapter to describe the generic algorithms in the standard library.
Automatic creation of function objects
The
To illustrate, let’s use only standard function objects to accomplish what gt15( ) did earlier. The standard function object, greater, is a
//: C06:CopyInts4.cpp
// Uses a standard function object and adapter
#include
#include
#include
#include
#include
using namespace std;
int main() {
int a[] = {10, 20, 30};
const size_t SIZE = sizeof a / sizeof a[0];
remove_copy_if(a, a + SIZE,
ostream_iterator
bind2nd(greater
} ///:~.
This program accomplishes the same thing as CopyInts3.cpp, but without our having to write our own predicate function gt15( ). The function object adapter bind2nd( ) is a template function that creates a function object of type binder2nd, which simply stores the two arguments passed to bind2nd( ), the first of which must be a binary function or function object (that is, anything that can be called with two arguments). The operator( ) function in binder2nd, which is itself a unary function, calls the binary function it stored, passing it its incoming parameter and the fixed value it stored.
To make the explanation concrete for this example, let’s call the instance of binder2nd created by bind2nd( ) by the name b. When b is created, it receives two parameters (greater
remove_copy_if(a, a + SIZE, o, b(g, 15).operator());
As remove_copy_if( ) iterates through the sequence, it calls b on each element, to determine whether to ignore the element when copying to the destination. If we denote the current element by the name e, that call inside remove_copy_if( ) is equivalent to.
if (b(e))
but binder2nd’s function call operator just turns around and calls g(e,15), so the earlier call is the same as.
if (greater
which is the comparison we were seeking. There is also a bind1st( ) adapter that creates a binder1st object, which fixes the