程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 從匯編的眼光看C++(之泛型編程)

從匯編的眼光看C++(之泛型編程)

編輯:C++入門知識

 

【 聲明:版權所有,歡迎轉載,請勿用於商業用途。  聯系信箱:feixiaoxing @163.com】

 

 

    泛型編程其實不難。本質上說,泛型編程就是讓通用的算法應用到所有的數據類型。具體來說,int是我們熟悉的整數類型。那麼一般情況下,如果我們寫一個int整數的排序算法,應該怎麼寫呢?大家可以自己試試?下面的代碼是我的一個范例;

void bubble_sort(int array[], int length) 

    int temp = 0; 

    int outer = 0; 

    int inner = 0; 

    assert(NULL != array && 0 != length); 

 

    for(outer = length -1; outer >= 1; outer --) 

    { 

        for(inner = 0; inner <= outer; inner ++) 

        { 

            if(array[inner] > array[inner + 1]) 

            { 

                temp = array[inner]; 

                array[inner] = array[inner + 1]; 

                array[inner + 1] = temp; 

            } 

        } 

    } 

 

    return; 

void bubble_sort(int array[], int length)

{

       int temp = 0;

       int outer = 0;

       int inner = 0;

       assert(NULL != array && 0 != length);

 

       for(outer = length -1; outer >= 1; outer --)

       {

              for(inner = 0; inner <= outer; inner ++)

              {

                     if(array[inner] > array[inner + 1])

                     {

                            temp = array[inner];

                            array[inner] = array[inner + 1];

                            array[inner + 1] = temp;

                     }

              }

       }

 

       return;

}   如果把數據類型改成通用的數據類型,你需要做什麼呢?兩個:(1)算術符"="重載;(2)比較函數。下面就是一個設計的class類型。

class type 

    int data; 

public: 

    type(int value = 0): data(value) {} 

    type(type& t) {data = t.get_data();} 

    ~type() {} 

    type& operator=(type& t) {data = t.get_data(); return *this;} 

    int get_data() {return data;} 

}; 

class type

{

       int data;

public:

       type(int value = 0): data(value) {}

       type(type& t) {data = t.get_data();}

       ~type() {}

       type& operator=(type& t) {data = t.get_data(); return *this;}

       int get_data() {return data;}

};    那麼,比較函數呢?我們可以用一個全局函數代替。

int type_compare(type& t1, type& t2) 

    return t1.get_data() > t2.get_data() ? 1 : 0; 

int type_compare(type& t1, type& t2)

{

       return t1.get_data() > t2.get_data() ? 1 : 0;

}    至此所有的函數都已經修改好了,那麼bubble_sort的函數也要修改吧,我們看看應該怎麼做?

template <typename data> 

void bubble_sort(data array[], int length, int (*compare)(data& , data& )) 

    data temp; 

    int outer = 0; 

    int inner = 0; 

    assert(NULL != array && 0 != length); 

 

    for(outer = length -1; outer >= 1; outer --) 

    { 

        for(inner = 0; inner <= outer; inner ++) 

        { 

            if(compare(array[inner], array[inner+1])) 

            { 

                temp = array[inner]; 

                array[inner] = array[inner + 1]; 

                array[inner + 1] = temp; 

            } 

        } 

    } 

 

    return; 

template <typename data>

void bubble_sort(data array[], int length, int (*compare)(data& , data& ))

{

       data temp;

       int outer = 0;

       int inner = 0;

       assert(NULL != array && 0 != length);

 

       for(outer = length -1; outer >= 1; outer --)

       {

              for(inner = 0; inner <= outer; inner ++)

              {

                     if(compare(array[inner], array[inner+1]))

                     {

                            temp = array[inner];

                            array[inner] = array[inner + 1];

                            array[inner + 1] = temp;

                     }

              }

       }

 

       return;

}    眼看著代碼都已經使用好了,那下面應該看看怎麼使用了。可以看看下面的代碼:

272:      type t[2] = {type(2), type(1)}; 

0040148D   push        2 

0040148F   lea         ecx,[ebp-14h] 

00401492   call        @ILT+25(type::type) (0040101e) 

00401497   mov         dword ptr [ebp-4],0 

0040149E   push        1 

004014A0   lea         ecx,[ebp-10h] 

004014A3   call        @ILT+25(type::type) (0040101e) 

004014A8   mov         byte ptr [ebp-4],1 

273:      bubble_sort<type> (t, 2, type_compare); 

004014AC   push        offset @ILT+20(type_compare) (00401019) 

004014B1   push        2 

004014B3   lea         eax,[ebp-14h] 

004014B6   push        eax 

004014B7   call        @ILT+50(bubble_sort) (00401037) 

004014BC   add         esp,0Ch 

274:      return; 

004014BF   mov         byte ptr [ebp-4],0 

004014C3   lea         ecx,[ebp-10h] 

004014C6   call        @ILT+5(type::~type) (0040100a) 

004014CB   mov         dword ptr [ebp-4],0FFFFFFFFh 

004014D2   lea         ecx,[ebp-14h] 

004014D5   call        @ILT+5(type::~type) (0040100a) 

275:  } 

272:      type t[2] = {type(2), type(1)};

0040148D   push        2

0040148F   lea         ecx,[ebp-14h]

00401492   call        @ILT+25(type::type) (0040101e)

00401497   mov         dword ptr [ebp-4],0

0040149E   push        1

004014A0   lea         ecx,[ebp-10h]

004014A3   call        @ILT+25(type::type) (0040101e)

004014A8   mov         byte ptr [ebp-4],1

273:      bubble_sort<type> (t, 2, type_compare);

004014AC   push        offset @ILT+20(type_compare) (00401019)

004014B1   push        2

004014B3   lea         eax,[ebp-14h]

004014B6   push        eax

004014B7   call        @ILT+50(bubble_sort) (00401037)

004014BC   add         esp,0Ch

274:      return;

004014BF   mov         byte ptr [ebp-4],0

004014C3   lea         ecx,[ebp-10h]

004014C6   call        @ILT+5(type::~type) (0040100a)

004014CB   mov         dword ptr [ebp-4],0FFFFFFFFh

004014D2   lea         ecx,[ebp-14h]

004014D5   call        @ILT+5(type::~type) (0040100a)

275:  }    我們看到了,簡單的排序已經完成了,函數最終會調用bubble_sort函數。泛型雖然復雜,涉及到了函數指針、算術符重載、模板函數等知識,但是只要勇於嘗試,就會使用越來越方便,越來越順手。

 

 

問題:

    (1) 大家可以嘗試編寫一個insert_sort的泛型函數?

    (2)嘗試編寫一個二分法的泛型處理函數?

    (3)嘗試編寫一個quick_sort的泛型函數,可能考慮的因素需要多一些?不過也可以嘗試一下哦。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved