Напишите связывающий адаптер binder3(), который должен связывать второй и третий аргументы трехаргументной функции для получения унарного предиката. Приведите пример полезного применения binder3().
От еле еле сделал, без посторонней помощи не обошлось, честно скажу оказалось тяжелое задание.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <iostream> using std::cout; using std::endl; #include <algorithm> using std::find_if; #include <functional> using std::unary_function; template<class Pred, class T> class binder3 : unary_function<T,bool> { Pred pr; T val; public: binder3(Pred pr1,T val1):pr(pr1),val(val1){} result_type operator()(const argument_type& i) const { return pr(i,val); } }; template<class Pred, class T> binder3<Pred,T> my_binder3(Pred pr,T val) { return binder3<Pred,T>(pr,val); } //бинарный предикат bool fun(int a,int b){return (a>b);} struct pr { bool operator()(int a,int b)const{return (a>b);} }pr1; int main() { int mass[]={1,2,3,4,5,6}; int* p=find_if(mass,mass+6,my_binder3(fun,3)); cout <<*p<<endl; p=find_if(mass,mass+6,my_binder3(pr1,3)); cout <<*p<<endl; return 0; } |
[youtube]http://www.youtube.com/watch?v=GJBSfJsrEpo[/youtube]