C++ 仿函數

來源:互聯網
上載者:User
感覺徹底的鬱悶了 整了好多的文法 竟然還有的文法沒有聽到過 真是見識了 看觀察者模式的例子時候出現了個仿函數 真是鬱悶 沒辦法 下面是找的內容 我暈了 不過還好理解了點點 仿函數就是在類裡面重載函數 重載函數的哪個地方我們就可以有參數 這樣的時候我們調用的時候就直接用類名加上哪個函數後面的參數就可以了 這樣的話就和直接調用函數是一個寫法 這就是叫仿函數的由來 也不知道是誰這樣弄的 麻煩人 還偏偏有人用 為了顯示懂得多吧  真是鬱悶人下面就是具體的例子://**
// * 所謂的仿函數(functor),是通過重載()運算子類比函數行為的類。
// * 因此,這裡需要明確兩點:
// * 1 仿函數不是函數,它是個類;
// * 2 仿函數重載了()運算子,使得它的對你可以像函數那樣子調用(代碼的形式好像是在調用函數)。
// **/  #include <iostream>  //標準輸入輸出資料流
using namespace std;//標準命名空間const int CMP_LES = -1;//定義的常量
const int CMP_EQU = 0;
const int CMP_BIG = 1;class Comparer  //仿函數的類 函數的主要功能是重載運算子()這樣調用的時候就直接用類引用加上()後面的參數就可以
{
public:
 Comparer(int cmpType)
    {
  m_cmpType = cmpType;//初始設定變數
    }    bool operator ()(int num1, int num2) const
    {
  bool res;
        switch(m_cmpType)
        {
   case CMP_LES:
    res = num1 < num2;
                break;
            case CMP_EQU:
                res = num1 == num2;
                break;
            case CMP_BIG:
                res = num1 > num2;
                break;
            default:
                res = false;
                break;
        }
  return res;
    }
private:
 int m_cmpType;
};//互相交換數值
void Swap(int &num1, int &num2)
{
 int temp = num1;
    num1 = num2;
    num2 = temp;
}//這個是個排序函數 雙層迴圈來排列順序
void SortArray(int array[], int size, const Comparer &cmp)
{
 for (int i = 0; i < size - 1; ++i)
 {
  int indx = i;
        for (int j = i + 1; j < size; ++j)
        {
   if (cmp(array[indx], array[j]))//這裡是對仿函數類的引用 看看很像函數
            {
    indx = j;
            }
        }
        if (indx != i)
        {
        Swap(array[i], array[indx]);
        }
 }
}//這是輸出顯示函數
void ListArray(int array[], int size)
{
 for (int i = 0; i < size; ++i)
    {
  cout << array[i] << " ";
    }
}#define ARY_SIZE 10
int main()
{
 int array[ARY_SIZE] = {10, 12, 9, 31, 93, 34, 98, 9, 1, 20};//定義一個數組 有十個資料 cout << "The initial array is : ";//首先輸出現在的數組資料
 ListArray(array, ARY_SIZE);
 cout << endl; SortArray(array, ARY_SIZE, Comparer(CMP_BIG));//然後在這裡用到了SortArray排序函數 其中第三個參數是個//對象的引用 所以要初始化這個對象(他是先初始化Comparer對象然後再動作的)
 cout << "The ascending sorted array is :";
 ListArray(array, ARY_SIZE);
 cout << endl; SortArray(array, ARY_SIZE, Comparer(CMP_LES));//用這個方法來排序
 cout << "The descending sorted array is : ";//輸出排序以後的方法
 ListArray(array, ARY_SIZE);
 cout << endl;
  return 0;
}//****************************************************************************************程式中定義了一個仿函數Comparer,它重重載了()運算子:
  Comparer::bool operator ()(int num1, int num2) const;
  這裡溫習一下運算子多載的方式:
  ret_type operator opt(array_list);
  其中,ret_type為運算子多載後傳回值的類型,operator為c++運算子多載專用關健字,opt為所要重載的運算子,如+, -, *, /, [], ()...
  於是我們可以解讀Comparer::bool operator ()(int num1, int num2) const的意義:
  bool限定了()的傳回值為布爾類型,(int num1, int num2)指定了運算子()的參數形式,const使得應該運算子可被它的const對象調用。()運算子中根據m_cmpType值返回不同方式下兩整數的比較值。

  函數void SortArray(int array[], int size, const Comparer &cmp)用於給數組排序。其中,array[]指定所要排序的數組對象,size限定數組元素個數,cmp為Comparer對象的引用,用作對元素的比較使用,前面使用const修飾是向函數調用都聲明,在函數內不會有修改該對象任何資料的形為。注意SortArray中的代碼:
            if (cmp(array[indx], array[j]))
            {
                indx = j;
            }
  其中,cmp為Comparer類的一個對象,但這裡的用法好像它是某個函數的樣子。這就是仿函數的真諦。

  別外,void Swap(int &num1, int &num2)完成交換num1與num2值的功能。int &num1表示函數參數使用的引用,用久了c的朋友也許更習慣了void Swap(int *num1, int *num2),但在c++中這個習慣要改了,引用和指標一樣高效,但引用要比指標更直觀。下面是指標版的Swap函數:
            void Swap(int *num1, int *num2)
            {
                int temp = *num1;
                *num1 = *num2;
                *num2 = temp;
            }
  實現的功能與程式中使用的一模一樣,替換掉程式照樣正常工作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.