仿函式(functor),就是使一個類的使用看上去像一個函式。其實現就是類中實現一個operator(),這個類就有了類似函式的行為,就是一個仿函式類了。
基本介紹
- 中文名:仿函式
- 外文名:functor
- 釋義:使一個類的使用看上去象一個函式
- 性質:編程術語
仿函式的概念與作用
//less的定義template<typename _Tp> struct less : public binary_function<_Tp, _Tp, bool> { bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; } };
//set的定義template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key> > class set;
仿函式在各程式語言中的範例
C
#include <stdlib.h>/* Callback function */int compare_ints_function(void*A,void*B){ return*((int*)(A))<*((int*)(B));}/* Declaration of C sorting function */void sort(void*first_item,size_t item_size,void*last_item,int(*cmpfunc)(void*,void*));int main(void){ int items[]={4,3,1,2}; sort((void*)(items),sizeof(int),(void*)(items +3), compare_ints_function); return 0;}
C++
class compare_class{public: bool operator()(int A, int B)const{return A < B;}};// Declaration of C++ sorting function.template<class ComparisonFunctor> void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);int main(){ int items[]={4, 3, 1, 2}; compare_class functor; sort_ints(items, sizeof(items)/sizeof(items[0]), functor);}
C#
Java
List<String> list =Arrays.asList("10", "1", "20", "11", "21", "12");Comparator<String> numStringComparator =new Comparator<String>(){ publicint compare(String o1, String o2){ returnInteger.valueOf(o1).compareTo(Integer.valueOf(o2)); }};Collections.sort(list, numStringComparator);
仿函式的實際套用C++
#include <iostream>#include <algorithm>#include <cstdio>#include <ctime>#include <cstring>#include <string>#include <set>typedef long long LL;class Prt{ char c[53]; int top;public: template <class T> Prt& operator()(T x); Prt& operator()(LL x); Prt& operator()(int x); Prt& operator()(char x); Prt& operator()(const char*x); Prt& operator()(double x); Prt& operator()(const std::string x); Prt& operator()(double x,int k){ sprintf(c,"%%.%dlf",k); printf(c,x); return *this; }};template <typename T>Prt& Prt::operator()(T x){ std::cout<<x; return *this;}Prt& Prt::operator()(LL x){ if(x==0){ putchar('0'); return *this; } if(x<0){ putchar('-'); x=-x; } top=0; while(x>0){ c[top++]='0'+x%10; x/=10; } while(top--){ putchar(c[top]); } return *this;}Prt& Prt::operator()(int x){ return operator()((LL)(x));}Prt& Prt::operator()(char x){ putchar(x); return *this;}Prt& Prt::operator()(const char*x){ printf("%s",x); return *this;}Prt& Prt::operator()(double x){ printf("%lf",x); return *this;}Prt& Prt::operator()(const std::string x){ return operator()(x.data());}Prt prt;struct st{int x,y;st(){x=y=0;}st(int a,int b){x=a;y=b;}};std::ostream& operator<<(std::ostream& fout,const st&x){ fout<<"["<<x.x<<","<<x.y<<"]"; return fout;}int main(){ prt(">>> prt(\"LL:\")(12341231234123ll)(\' \')(\"int:\")(15)(\'\\n\');\n"); prt("LL:")(12341231234123ll)(' ')("int:")(15)('\n'); prt('\n'); prt(">>> prt(\"char:\")(\'a\')(\" char*(/string):\")(std::string(\"abc\"))(\" d \")\((std::string(\"abc\")).data())(\'\\n\');\n"); prt("char:")('a')(" char*(/string):")(std::string("abc"))(" d ") ((std::string("abc")).data())('\n'); prt('\n'); prt(">>> prt(\"double:\")(1.5)(\" double[int]:\")(10.12349746192736,4)(\'\\n\');\n"); prt("double:")(1.5)(" double[int]:")(10.12349746192736,4)('\n'); prt("\n>>> prt(st(12,31));\n"); prt(st(12,31)); prt('\n'); return 0;}