标签:
1、模板的概念
我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同。正确的调用重载函数。例如,为求两个数的最大值,我们定义MAX()函数需要对不同的数据类型分别定义不同重载(Overload)版本。
//函数1.
int max(int x,int y)
{  return(x>y)?x:y ; }
//函数2.
float max( float x,float y)
{  return (x>y)? x:y ; }
//函数3.
double max(double x,double y)
{  return (x>y)? x:y ; }
但如果在主函数中,我们分别定义了 char a,b; 那么在执行max(a,b);时 程序就会出错,因为我们没有定义char类型的重载版本。
现在,我们再重新审视上述的max()函数,它们都具有同样的功能,即求两个数的最大值,能否只写一套代码解决这个问题呢?这样就会避免因重载函数定义不 全面而带来的调用错误。为解决上述问题C++引入模板机制,模板定义:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。
2、 函数模板的写法
函数模板的一般形式如下:
Template <class或者也可以用typename T>
返回类型 函数名(形参表)
{//函数定义体 }
说明: template是一个声明模板的关键字,表示声明一个模板关键字class不能省略,如果类型形参多余一个 ,每个形参前都要加class <类型 形参表>可以包含基本数据类型可以包含类类型。
请看以下程序:
- 
#include <iostream>  
- 
using std::cout;  
- 
using std::endl;  
- 
  
- 
  
- 
template <class T>  
- 
T min(T x,T y)  
- 
{  
- 
    return(x<y)?x:y;  
- 
}  
- 
void main( )  
- 
{  
- 
  
- 
     int n1=2,n2=10;  
- 
     double d1=1.5,d2=5.6;  
- 
     cout<< "较小整数:"<<min(n1,n2)<<endl;  
- 
     cout<< "较小实数:"<<min(d1,d2)<<endl;  
- 
     system("pause");  
- 
}  
 
程序运行结果:

 
 
 
 
 
 
 
1 、模板类和重载函数一起使用
     两者一起使用时,先考虑重载函数,后考虑模板类,如过再找不到,就考虑类型转换,可能会带来精度的变化。
- 
#include "iostream"  
- 
using namespace std ;  
- 
  
- 
  
- 
template <class T>    
- 
const T MAX(T a , T b)  
- 
{  
- 
    printf("%s\n" , "template") ;  
- 
    return (a > b) ? a : b ;       
- 
  
- 
}  
- 
  
- 
int MAX(int x , int y)  
- 
{  
- 
    printf("%s\n" , "int int" );  
- 
    return (x > y) ? x : y ;       
- 
}   
- 
  
- 
int MAX(char x , int y)  
- 
{  
- 
    printf("%s\n" , "char int" );  
- 
    return (x > y) ? x : y ;       
- 
}   
- 
  
- 
int MAX(int x , char y)  
- 
{  
- 
    printf("%s\n" , "int char" );  
- 
    return (x > y) ? x : y ;      
- 
}   
- 
  
- 
int main(void)  
- 
{    
- 
    int a = 3 , b = 5 ;  
- 
    char x = ‘x‘ ;   
- 
    double c = 3.4  ;  
- 
    cout<<MAX(a , b)<<endl ;       
- 
    cout<<MAX(c , b)<<endl ;       
- 
  
- 
    cout<<MAX(a , x)<<endl ;       
- 
    cout<<MAX(x , a)<<endl ;       
- 
    cout<<MAX(c , a)<<endl ;  
- 
    cout<<MAX(a) ;  
- 
    system("pause") ;   
- 
    return 0 ;     
- 
}  
 
2 、类模板
    (1)类模板的具体格式
        template <class T>
        class A
       {
       }
      在类定义体外定义的成员函数,应该使用函数模板。
- 
 
- 
 
- 
  
- 
#include <iostream>  
- 
using namespace std ;  
- 
template <class T>  
- 
class Base  
- 
{  
- 
public :      
- 
    T a ;  
- 
    Base(T b)  
- 
    {  
- 
        a = b ;      
- 
    }     
- 
    T getA(){ return a ;}   
- 
    void setA(T c);  
- 
};  
- 
  
- 
template <class T>     
- 
void  Base<T>::setA(T c)  
- 
{  
- 
    a = c ;  
- 
}  
- 
  
- 
int main(void)  
- 
{  
- 
    Base <int>b(4);  
- 
    cout<<b.getA()<<endl;   
- 
  
- 
    Base <double> bc(4);  
- 
    bc.setA(4.3);  
- 
    cout<<bc.getA()<<endl;   
- 
    system("pause");   
- 
    return 0 ;      
- 
}  
 
注意成员函数在类外定义的情况。
3 、模板类
  主要指的是 STL 模板类
参考出处:http://blog.csdn.net/hackbuteer1/article/details/6735704c++模板类学习简单
标签:
原文地址:http://blog.csdn.net/ghevinn/article/details/43191855