C++仿函式

gaopengtttt發表於2017-04-04

所謂仿函式就是和函式呼叫非常類似的一種呼叫方式,實際上仿函式只是過載了()運算子,
這種方式在STL容器函式中使用非常普遍,其中又分為函式物件和謂詞

class t
{
public:
void operator()(stu& a) 函式物件(一元)
/*
bool operator()(stu& a) 謂詞(一元),謂詞只會放回布林值
*/
};
void test(stu& a) 函式

那麼呼叫我們可以很清楚的可以看出
仿函式呼叫為
t lfun;
lfun(a); 
其中lfun為定義的類物件而已
函式呼叫為
test(a);

他們的呼叫看起來及其相似。

下面演示仿函式的使用方式

點選(此處)摺疊或開啟

  1. /*************************************************************************
  2.     > File Name: 仿函式.cpp
  3.     > Author: gaopeng QQ:22389860 all right reserved
  4.     > Mail: gaopp_200217@163.com
  5.     > Created Time: Sun 23 Apr 2017 08:03:41 PM CST
  6.  ************************************************************************/

  7. #include<iostream>
  8. #include<vector>
  9. #include<algorithm>
  10. #include<string.h>
  11. using namespace std;




  12. class testfun //仿函式
  13. {
  14.         public:
  15.                 testfun(void)
  16.                 {
  17.                         cnt = 0;
  18.                 }
  19.                 void operator()(int& a)
  20.                         {
  21.                                 cnt++;
  22.                                 if( !(a%67))
  23.                                 {
  24.                                         cout<<a <<endl;
  25.                                 }
  26.                         }
  27.                 int cnt;
  28. };


  29. class stu
  30. {
  31.         private:
  32.                 char name[20];
  33.                 int age;
  34.                 friend class stufun;
  35.         public:
  36.                 stu(const char* inc,int b)
  37.                 {
  38.                         strcpy(name,inc);
  39.                         age = b;
  40.                 }

  41. };

  42. class stufun
  43. {
  44.         public:
  45.                 int equ;
  46.         public:
  47.                 stufun(int m):equ(m){} //建構函式,仿函式中可以儲存任何比較條件 這是仿函式(函式物件或者謂詞)和函式指標進行傳遞到STL函式的區別,因為仿函式更加方便
  48.                 /*
  49.                 void operator()(stu& a) //仿函式 一元函式物件 stufun(m)比較比m大的值 stu&a代表是STL函式會將每一個容器物件 stu 透過引用傳入到a中然後一一進行比較
  50.                 {
  51.                         if(a.age == equ)
  52.                         {
  53.                                 cout<<a.name<<endl;
  54.                                 cout<<a.age<<endl;
  55.                         }
  56.                 }
  57.                 */
  58.                 bool operator()(stu& a) //一元謂詞 stu&a代表是STL函式會將每一個容器物件 stu 透過引用傳入到a中然後一一進行比較
  59.                 {
  60.                         if(a.age == equ)
  61.                         {
  62.                                 cout<<a.name<<endl;
  63.                                 cout<<a.age<<endl;
  64.                                 return true;
  65.                         }
  66.                         else
  67.                         {
  68.                                 return false;
  69.                         }
  70.                 }

  71. };


  72. void kkfun(int& a)
  73. {
  74.         if( !(a%67))
  75.         {
  76.                 cout<<a <<endl;
  77.         }
  78. }

  79. int main(void)
  80. {
  81.         cout<<"test1----"<<endl;
  82.         vector<int> m;
  83.         for(int i = 0;i<999;i++)
  84.         {
  85.                 m.push_back(i);
  86.         }
  87.         testfun l;
  88.     l = for_each(m.begin(),m.end(),testfun());//呼叫仿函式 匿名函式物件 進行複製需要接回來

  89.         for_each(m.begin(),m.end(),kkfun);//呼叫函式指標
  90.         cout<<"test2----"<<endl;

  91.         vector<stu> ii;
  92.         stu a("gaopeng",31);
  93.         stu b("yanllei",30);
  94.         stu c("gzh",3);
  95.         stu d("test",31);
  96.         ii.push_back(a);
  97.         ii.push_back(b);
  98.         ii.push_back(c);
  99.         ii.push_back(d);

  100.         //for_each(ii.begin(),ii.end(),stufun());
  101.         stufun o(3);
  102.         for_each(ii.begin(),ii.end(),o);//呼叫謂詞 定義的函式物件o

  103. // stufun o;
  104. // o(a);


  105.         return 0;

  106. }



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2136560/,如需轉載,請註明出處,否則將追究法律責任。

相關文章