哦,这倒是挺有意思的。于是依葫芦画瓢:
bool print(int i, int j)
{
std::cout<< i << "---" << j << std::endl;
return i>j;
}
int main(int argc, char *argv[])
{
(std::bind1st(print, 2))(1);
return 0;
}
满怀希望它能够打印
2---1
只不过。。。编译出错:
1 Error error C2784: 'std::binder1st<_Fn2> std::bind1st(const_Fn2 &,const _Ty &)' : could not deduce template argument for'overloaded function type' from 'overloaded function type'
---不能够推断出模板参数for 'overloaded function type' from 'overloaded function type'。。。。
(还真看不明白。。。)
Helpertemplate functions used to convert unary and binary function pointers,respectively, into unary and binary adaptable functions.
template<class Arg, class Result>
pointer_to_unary_function<Arg, Result, Result (*)(Arg)>
ptr_fun(Result (*_pfunc)(Arg));
template<class Arg1, class Arg2, class Result>
pointer_to_binary_function<Arg1, Arg2, Result, Result (*)(Arg1, Arg2)>
ptr_fun(Result (*_pfunc)(Arg1, Arg2));
Parameters
_pfunc The unary or binary function pointer to be converted to an adaptable function.
Return Value
The first template function returns the unary function pointer_to_unary_function <Arg, Result>(*_pfunc).
The second template function returns binary function pointer_to_binary_function <Arg1, Arg2, Result>(*_pfunc).
Remarks
A function pointer is a function objectand may be passed to any Standard Template Library algorithm that isexpecting a function as a parameter, but it is not adaptable. To use itwith an adaptor, such as binding a value to it or using it with anegator, it must be supplied with the nested types that make such anadaptation possible. The conversion of unary and binary functionpointers by the ptr_fun helper function allows the function adaptors to work with unary and binary function pointers.
// To search the sequence for "pearly"
// use a pointer_to_function conversion
RIter = find_if( v1.begin( ), v1.end( ),
not1 ( bind2nd (ptr_fun ( strcmp ), "pearly" ) ) );
if ( RIter != v1.end( ) )
{
cout << "The search for 'pearly' was successful.\n";
cout << "The next character string is: "
<< *++RIter << "." << endl;
}
}
Output
Original sequence contains: Open up the pearly gates
The search for 'pearly' was successful.
The next character string is: gates.