函数模板-17

一。泛型编程的概念

  1.不考虑具体数据类型的编程模式

   对于Swap函数可以考虑下面的泛型写法

void Swap(T&a, T&b)
{
       T t = a;
         a = b;
         b = t;     
  
}

  2.函数模板的语法规则

  -template关键字用于声明开始进行的泛型编程

  -typename关键字用于声明反省

  template <typename T>

 3.函数模板的应用

  -自动类型推倒调用

  -具体类型显示调用

#include <cstdlib>
#include <iostream>

using namespace std;

template <typename T>
void Swap(T&a,T&b)
{
    T t = a;
      a = b;
      b = t;  
    
}
int main(int argc, char *argv[])
{
    int a = 1;
    int b = 2;
    
    Swap(a,b);
    cout << a <<" "<< b <<endl;
    
    float fa = 3;
    float fb = 4;
    Swap(fa,fb);
    cout << fa <<" "<< fb <<endl;
    
    char ca = 'ca';
    char cb = 'cb';
    Swap(ca,cb);
    cout << ca <<" "<< cb <<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

二。泛型算法

#include <cstdlib>
#include <iostream>

using namespace std;

template<typename T>
void Swap(T& a, T& b)
{
    T t = a;
    a = b;
    b = t;
}

template<typename T>
void SelectSort(T array[], int length)
{
    for(int i=0; i<length; i++)
    {
        T min = array[i];
        int index = i;
        
        for(int j=i+1; j<length; j++)
        {
            if( array[j] < min )
            {
                min = array[j];
                index = j;
            }
        }
        
        Swap(array[i], array[index]);
    }
}

int main(int argc, char *argv[])
{
    int array[] = {3, 2, 5, 3 , 4};
    
    SelectSort<int>(array, 5);
    
    for(int i=0; i<5; i++)
    {
        cout<<array[i]<<endl;
    }
    
    char ca[] = {'b', 'c', 'a', 'e', 'd', 'f'};
    
    SelectSort(ca, 6);
    
    for(int i=0; i<6; i++)
    {
        cout<<ca[i]<<endl;
    }
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

三。函数模板与重载

  函数模板可以像普通函数一样被重载

  1.c++编译器优先考虑普通

  2.如果函数模板可以产生一个更好的匹配,那么选择模板

  3.可以通过空模板实参列表的语法限定编译器只通过模板匹配

#include <cstdlib>
#include <iostream>

using namespace std;

int Max(int a, int b)
{
    cout<<"int Max(int a, int b)"<<endl;
    return a > b ? a : b;
}

template<typename T>
T Max(T a, T b)
{
    cout<<"T Max(T a, T b)"<<endl;
    return a > b ? a : b;
}

template<typename T>
T Max(T a, T b, T c)
{
    cout<<"T Max(T a, T b, T c)"<<endl;
    return Max(Max(a, b), c);
}


int main(int argc, char *argv[])
{
    int a = 1;
    int b = 2;
    
    cout<<Max(a, b)<<endl;
    cout<<Max<>(a, b)<<endl;
    
    cout<<Max(3.0, 4.0)<<endl;
    
    cout<<Max(5.0, 6.0, 7.0)<<endl;
    
    cout<<Max('a', 100)<<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

四。注意事项

#include <cstdlib>
#include <iostream>

using namespace std;


template<typename RT, typename T1, typename T2>
RT Add(T1 a, T2 b)
{
    return static_cast<RT>(a + b);
}


int main(int argc, char *argv[])
{
    cout<<Add<double, char, float>('a', 100.0f)<<endl;
    cout<<Add<double>('a', 100.0f)<<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

 

 1.函数模板不允许自动类型转化的

  2.普通函数能够进行自动类型转换

 

posted @ 2017-10-15 16:16  吕晓宁  阅读(162)  评论(0编辑  收藏  举报