Function objects are objects with an operator()(..) defined. This results in faster code than passing function pointers, as the operator can even be inlined. The following function object classes are defined in \stl.
#include <function.h>
\stl defines the following function object classes plus<T>, minus<T>, times<T>, divides<T>, and modulus<T>, which have an operator() with two arguments. Furthermore, there is the function object class negate<T> with a single argument. The arguments as well as the return value are of type T.
Pars pro toto we give the more formal definition for tha class plus<T>.
{ const int n = 10; int A[n]; A[0] = 23; ... A[9] = 56; for_each(A, A+n, negate<int>()); }
#include <function.h>
\stl defines the following function object classes equal_to<T>, not_equal_to<T>, greater<T>, less<T>, greater_equal<T>, less_equal<T>. They all have an operator() with two arguments of type T and the return value is of type bool.
{ set< int, greater<int> > S; }
The following code fragment shows how to sort an array using the \stl function sort.
{ const int n = 10; int A[n]; A[0] = 23; ... A[9] = 56; sort(A, A+n, greater<int>()); }