.\" ident @(#)Negators.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH Negators 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2Negators\fP \ - Function adaptors and function objects used to reverse the sense of predicate function objects. .SH SYNOPSIS .br #include .br template .br class unary_negate; .br .br template .br unary_negate not1(const Predicate&); .br .br template .br class binary_negate; .br .br template .br binary_negate not2(const Predicate&); .SH DESCRIPTION Negators not1 and not2 are functions that take predicate function objects as arguments and return predicate function objects with the opposite sense. Negators work only with function objects defined as subclasses of the classes unary_function and binary_function. not1 accepts and returns unary predicate function objects. not2 accepts and returns binary predicate function objects. unary_negate and binary_negate are function object classes that include return types for the negators, not1 and not2. .SH INTERFACE .br template .br class unary_negate .RE .RS 2 : public unary_function .RE .RS 11 { .RE .RS 0 .br public: .RE .RS 1 explicit unary_negate (const Predicate&); .br bool operator() (const argument_type&) const; .RE .RS 0 }; .br .br template .br unary_negate not1 (const Predicate&); .br .br template .br class binary_negate .RE .RS 2 : public binary_function .RE .RS 11 .RE .RS 0 { .br public: .RE .RS 1 explicit binary_negate (const Predicate&); .br bool operator() (const first_argument_type&, .RE .RS 18 const second_argument_type&) const; .RE .RS 0 }; .br .br template .br binary_negate not2 (const Predicate&); .SH EXAMPLE .br // .br // negator.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .RE .RS 1 //Create a new predicate from unary_function .RE .RS 0 template .br class is_odd : public unary_function .RE .RS 1 { .RE .RS 2 public: .br bool operator()(const Arg& arg1) const .RE .RS 3 { .RE .RS 4 return (arg1 % 2 ? true : false); .RE .RS 3 } .RE .RS 1 }; .RE .RS 0 .br int main() .RE .RS 1 { .RE .RS 2 less less_func; .RE .RS 0 .RE .RS 3 // Use not2 on less .RE .RS 2 cout << (less_func(1,4) ? "TRUE" : "FALSE") << endl; .br cout << (less_func(4,1) ? "TRUE" : "FALSE") << endl; .br cout << (not2(less())(1,4) ? "TRUE" : "FALSE") .RE .RS 8 << endl; .RE .RS 2 cout << (not2(less())(4,1) ? "TRUE" : "FALSE") .RE .RS 8 << endl; .RE .RS 0 .RE .RS 3 //Create an instance of our predicate .RE .RS 2 is_odd odd; .RE .RS 0 .RE .RS 3 // Use not1 on our user defined predicate .RE .RS 2 cout << (odd(1) ? "TRUE" : "FALSE") << endl; .br cout << (odd(4) ? "TRUE" : "FALSE") << endl; .br cout << (not1(odd)(1) ? "TRUE" : "FALSE") << endl; .br cout << (not1(odd)(4) ? "TRUE" : "FALSE") << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br TRUE .br FALSE .br FALSE .br TRUE .br TRUE .br FALSE .br FALSE .br TRUE .SH SEE ALSO Algorithms, binary_function, Function_Objects, unary_function