.\" ident @(#)bind1st.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH bind1st 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2bind1st\fP, \f2bind2nd\fP, \f2binder1st\fP, \f2binder2nd\fP \ - Templatized utilities to bind values to function objects. .SH SYNOPSIS .br #include .br template .br class binder1st : public unary_function ; .RE .RS 0 .br template .br binder1st bind1st (const Operation&, const T&); .br template .br class binder2nd : public unary_function ; .RE .RS 0 .br template .br binder2nd bind2nd (const Operation&, const T&); .SH DESCRIPTION Because so many functions included in the standard library take other functions as arguments, the library includes classes that let you build new function objects out of old ones. Both \f2bind1st()\fP and \f2bind2nd()\fP are functions that take as arguments a binary function object \f2f\fP and a value \f2x,\fP and return, respectively, classes binder1st and binder2nd. The underlying function object must be a subclass of binary_function. Class binder1st binds the value to the first argument of the binary function, and binder2nd does the same thing for the second argument of the function. The resulting classes can be used in place of a unary predicate in other function calls. For example, you could use the count_if algorithm to count all elements in a vector that are less than or equal to 7, using the following: .br \f2count_if (v.begin, v.end, bind1st(greater (),7),\fP .br \f2 littleNums)\fP This function adds one to \f2littleNums\fP each time the predicate is \f2true\fP, in other words, each time 7 is greater than the element. .SH INTERFACE .br // Class binder1st .br template .br class binder1st .RE .RS 3 : public unary_function .RE .RS 0 { .br public: .br .RE .RS 2 binder1st(const Operation&, .RE .RS 12 const typename .br Operation::first_argument_type&); .RE .RS 2 typename Operation::result_type operator() .RE .RS 11 (const typename Operation::second_argument_type&) .br const; .RE .RS 0 }; .br .br // Class binder2nd .br template .br class binder2nd .RE .RS 3 : public unary_function .RE .RS 0 { .br public: .br .RE .RS 2 binder2nd(const Operation&, .RE .RS 12 const typename .br Operation::second_argument_type&); .RE .RS 2 typename Operation::result_type operator() .RE .RS 11 (const typename Operation::first_argument_type&) .br const; .RE .RS 0 }; .br .br // Creator bind1st .br .RE .RS 2 template .br binder1st bind1st (const Operation&, .RE .RS 32 const T&); .RE .RS 0 .br // Creator bind2nd .RE .RS 1 .RE .RS 2 template .br binder2nd bind2nd(const Operation&, .RE .RS 32 const T&); .SH EXAMPLE .RE .RS 0 // .br // binders.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br int main() .RE .RS 1 { .RE .RS 2 typedef vector::iterator iterator; .br int d1[4] = {1,2,3,4}; .RE .RS 3 // .br // Set up a vector .br // .RE .RS 2 vector v1(d1,d1 + 4); .RE .RS 3 // .br // Create an 'equal to 3' unary predicate by binding 3 to .br // the equal_to binary predicate. .br // .RE .RS 2 binder1st > equal_to_3 = .RE .RS 5 bind1st(equal_to(),3); .RE .RS 3 // .br // Now use this new predicate in a call to find_if .br // .RE .RS 2 iterator it1 = find_if(v1.begin(),v1.end(),equal_to_3); .RE .RS 3 // .br // Even better, construct the new predicate on the fly .br // .RE .RS 2 iterator it2 = .RE .RS 5 find_if(v1.begin(),v1.end(),bind1st(equal_to(),3)); .RE .RS 3 // .br // And now the same thing using bind2nd .br // Same result since == is commutative .br // .RE .RS 2 iterator it3 = .RE .RS 5 find_if(v1.begin(),v1.end(),bind2nd(equal_to(),3)); .RE .RS 3 // .br // it3 = v1.begin() + 2 .br // .br // Output results .br // .RE .RS 2 cout << *it1 << " " << *it2 << " " << *it3 << endl; .br return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 3 3 3 .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 >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO Function_Objects