用汇编的眼光看C++(之泛型编程)20

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】 


    泛型编程其实不难。本质上说,泛型编程就是让通用的算法应用到所有的数据类型。具体来说,int是我们熟悉的整数类型。那么一般情况下,如果我们写一个int整数的排序算法,应该怎么写呢?大家可以自己试试?下面的代码是我的一个范例;

  1. void bubble_sort(int array[], int length)  
  2. {  
  3.     int temp = 0;  
  4.     int outer = 0;  
  5.     int inner = 0;  
  6.     assert(NULL != array && 0 != length);  
  7.   
  8.     for(outer = length -1; outer >= 1; outer --)  
  9.     {  
  10.         for(inner = 0; inner <= outer; inner ++)  
  11.         {  
  12.             if(array[inner] > array[inner + 1])  
  13.             {  
  14.                 temp = array[inner];  
  15.                 array[inner] = array[inner + 1];  
  16.                 array[inner + 1] = temp;  
  17.             }  
  18.         }  
  19.     }  
  20.   
  21.     return;  
  22. }  

   如果把数据类型改成通用的数据类型,你需要做什么呢?两个:(1)算术符"="重载;(2)比较函数。下面就是一个设计的class类型。

  1. class type  
  2. {  
  3.     int data;  
  4. public:  
  5.     type(int value = 0): data(value) {}  
  6.     type(type& t) {data = t.get_data();}  
  7.     ~type() {}  
  8.     type& operator=(type& t) {data = t.get_data(); return *this;}  
  9.     int get_data() {return data;}  
  10. };  

    那么,比较函数呢?我们可以用一个全局函数代替。

  1. int type_compare(type& t1, type& t2)  
  2. {  
  3.     return t1.get_data() > t2.get_data() ? 1 : 0;  
  4. }  

    至此所有的函数都已经修改好了,那么bubble_sort的函数也要修改吧,我们看看应该怎么做?

  1. template <typename data>  
  2. void bubble_sort(data array[], int length, int (*compare)(data& , data& ))  
  3. {  
  4.     data temp;  
  5.     int outer = 0;  
  6.     int inner = 0;  
  7.     assert(NULL != array && 0 != length);  
  8.   
  9.     for(outer = length -1; outer >= 1; outer --)  
  10.     {  
  11.         for(inner = 0; inner <= outer; inner ++)  
  12.         {  
  13.             if(compare(array[inner], array[inner+1]))  
  14.             {  
  15.                 temp = array[inner];  
  16.                 array[inner] = array[inner + 1];  
  17.                 array[inner + 1] = temp;  
  18.             }  
  19.         }  
  20.     }  
  21.   
  22.     return;  
  23. }  

    眼看着代码都已经使用好了,那下面应该看看怎么使用了。可以看看下面的代码:

  1. 272:      type t[2] = {type(2), type(1)};  
  2. 0040148D   push        2  
  3. 0040148F   lea         ecx,[ebp-14h]  
  4. 00401492   call        @ILT+25(type::type) (0040101e)  
  5. 00401497   mov         dword ptr [ebp-4],0  
  6. 0040149E   push        1  
  7. 004014A0   lea         ecx,[ebp-10h]  
  8. 004014A3   call        @ILT+25(type::type) (0040101e)  
  9. 004014A8   mov         byte ptr [ebp-4],1  
  10. 273:      bubble_sort<type> (t, 2, type_compare);  
  11. 004014AC   push        offset @ILT+20(type_compare) (00401019)  
  12. 004014B1   push        2  
  13. 004014B3   lea         eax,[ebp-14h]  
  14. 004014B6   push        eax  
  15. 004014B7   call        @ILT+50(bubble_sort) (00401037)  
  16. 004014BC   add         esp,0Ch  
  17. 274:      return;  
  18. 004014BF   mov         byte ptr [ebp-4],0  
  19. 004014C3   lea         ecx,[ebp-10h]  
  20. 004014C6   call        @ILT+5(type::~type) (0040100a)  
  21. 004014CB   mov         dword ptr [ebp-4],0FFFFFFFFh  
  22. 004014D2   lea         ecx,[ebp-14h]  
  23. 004014D5   call        @ILT+5(type::~type) (0040100a)  
  24. 275:  }  

    我们看到了,简单的排序已经完成了,函数最终会调用bubble_sort函数。泛型虽然复杂,涉及到了函数指针、算术符重载、模板函数等知识,但是只要勇于尝试,就会使用越来越方便,越来越顺手。


问题:
    (1) 大家可以尝试编写一个insert_sort的泛型函数?
    (2)尝试编写一个二分法的泛型处理函数?
    (3)尝试编写一个quick_sort的泛型函数,可能考虑的因素需要多一些?不过也可以尝试一下哦。


【预告: 下面的博客会写一些 关于class技巧的文章】

posted @ 2012-01-18 11:14  董雨  阅读(189)  评论(0编辑  收藏  举报