#include <iostream> #include <functional> using namespace std; /** * 함수객체구현및 STL함수객체(less,greater, plus,minus) */ class Less{ public: bool operator()(int n1, int n2){ return n1 < n2 ? true : false; } }; class Greater{ public: bool operator()(int n1, int n2){ return n1 > n2 ? true : false; } }; class Plus{ public: int operator()(int n1, int n2){ return n1+n2; } }; class Minus{ public: int operator()(int n1, int n2){ return n1-n2; } }; typedef less<int> stl_less; typedef greater<int> stl_greater; typedef plus<int> stl_plus; typedef minus<int> stl_minus; int main() { Less l; stl_less l2; Greater g; stl_greater g2; Plus p; stl_plus p2; Minus m; stl_minus m2; cout<< Less()(10,20) <<endl; // 임시객체로 암묵적 함수호출 cout<< l.operator()(20,10)<<endl; // l1객체로 명시적 함수호출 cout<< stl_less()(10, 20) <<endl; // stl less 임시함수객체로 암묵적 함수호출 cout<< l2.operator ()(20, 10)<<endl;// stl less l2 함수객체로 명시적 함수호출 cout<<endl; cout<< Greater()(10, 20) <<endl; cout<< g.operator()(20, 10) <<endl; cout<< stl_greater()(10, 20) <<endl; cout<< g2.operator ()(20, 10)<<endl; cout<<endl; cout<< Plus()(10, 20) <<endl; cout<< p.operator()(20,10) <<endl; cout<< stl_plus()(10, 20)<<endl; cout<< p2.operator ()(20, 10) <<endl; cout<<endl; cout<< Minus()(10, 20) <<endl; cout<< m.operator()(20, 10) <<endl; cout<< stl_minus()(10, 20) <<endl; cout<< m2.operator ()(20, 10)<<endl; system("pause"); return 0; } |