.\" ident @(#)Function_Objects.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH Function_Objects 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2Function_Objects\fP \ - Function objects are objects with an \f2operator()\fP defined. They are used as arguments to templatized algorithms, in place of pointers to functions. .SH SYNOPSIS .RE .RS 1 #include .SH DESCRIPTION Function objects are objects with an \f2operator()\fP defined. They are important for the effective use of the standard library's generic algorithms, because the interface for each algorithmic template can accept either an object with an \f2operator()\fP defined, or a pointer to a function. The Standard C++ Library includes both a standard set of function objects, and a pair of classes that you can use as the base for creating your own function objects. Function objects that take one argument are called unary function objects. Unary function objects must include the typedefs \f2argument_type\fP and \f2result_type\fP. Similarly, function objects that take two arguments are called binary function objects and, as such, must include the typedefs \f2first_argument_type\fP, \f2second_argument_type\fP, and \f2result_type\fP. The classes \f2unary_function\fP and \f2binary_function\fP make the task of creating templatized function objects easier. The necessary typedefs for a unary or binary function object are included by inheriting from the appropriate function object class. The function objects in the standard library are listed below, together with a brief description of their operation. This class reference also includes an alphabetic entry for each function. NAME OPERATION \f2arithmetic functions\fP \f2plus\fP \f2addition x + y\fP \f2minus\fP \f2subtraction x - y\fP \f2multiplies\fP \f2multiplication x * y\fP \f2divides\fP \f2division x / y\fP \f2modulus\fP \f2remainder x % y\fP \f2negate\fP \f2negation - x\fP \f2comparison functions\fP \f2equal_to\fP \f2equality test x == y\fP \f2not_equal_to\fP \f2inequality test x != y\fP \f2greater\fP \f2greater comparison x > y\fP \f2less\fP \f2less-than comparison x < y\fP \f2greater_equal\fP \f2greater than or equal comparison x >= y\fP \f2less_equal\fP \f2less than or equal comparison x <= y\fP \f2logical functions\fP \f2logical_and\fP \f2logical conjunction x && y\fP \f2logical_or\fP \f2logical disjunction x || y\fP \f2logical_not\fP \f2logical negation ! x\fP .SH INTERFACE .RE .RS 3 template .br struct unary_function{ .RE .RS 8 typedef Arg argument_type; .br typedef Result result_type; .RE .RS 4 }; .RE .RS 0 .RE .RS 3 template .br struct binary_function{ .RE .RS 8 typedef Arg1 first_argument_type; .br typedef Arg2 second_argument_type; .br typedef Result result_type; .RE .RS 4 }; .RE .RS 0 .RE .RS 1 // Arithmetic Operations .RE .RS 0 .RE .RS 2 template .br struct plus : binary_function { .RE .RS 7 T operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct minus : binary_function { .RE .RS 7 T operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct multiplies : binary_function { .RE .RS 7 T operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct divides : binary_function { .RE .RS 7 T operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct modulus : binary_function { .RE .RS 7 T operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct negate : unary_function { .RE .RS 7 T operator() (const T&) const; .RE .RS 0 }; .br .RE .RS 1 // Comparisons .RE .RS 0 .br template .br struct equal_to : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct not_equal_to : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct greater : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct less : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct greater_equal : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct less_equal : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .RE .RS 1 // Logical Comparisons .RE .RS 0 .br template .br struct logical_and : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct logical_or : binary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .br .br template .br struct logical_not : unary_function { .RE .RS 8 bool operator() (const T&, const T&) const; .RE .RS 0 }; .SH EXAMPLE .br // .br // funct_ob.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .RE .RS 1 //Create a new function object from unary_function .RE .RS 0 template .br class factorial : public unary_function .RE .RS 1 { .RE .RS 2 public: .RE .RS 0 .RE .RS 2 Arg operator()(const Arg& arg) .RE .RS 3 { .RE .RS 4 Arg a = 1; .br for(Arg i = 2; i <= arg; i++) .RE .RS 6 a *= i; .RE .RS 4 return a; .RE .RS 3 } .RE .RS 1 }; .RE .RS 0 .br int main() .RE .RS 1 { .RE .RS 3 //Initialize a deque with an array of ints .RE .RS 2 int init[7] = {1,2,3,4,5,6,7}; .br deque d(init, init+7); .RE .RS 0 .RE .RS 3 //Create an empty vector to store the factorials .RE .RS 2 vector v((size_t)7); .RE .RS 0 .RE .RS 3 //Transform the numbers in the deque to their factorials .br //and store in the vector .RE .RS 2 transform(d.begin(), d.end(), v.begin(), .RE .RS 13 factorial()); .RE .RS 0 .RE .RS 3 //Print the results .RE .RS 2 cout << "The following numbers: " << endl << " "; .br copy(d.begin(),d.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 0 .RE .RS 2 cout << endl << endl; .br cout << "Have the factorials: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br The following numbers: .RE .RS 4 1 2 3 4 5 6 7 .RE .RS 0 Have the factorials: .RE .RS 4 1 2 6 24 120 720 5040 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector > and deque >\fP instead of: \f2vector and deque\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO binary_function, unary_function