码迷,mamicode.com
首页 > 编程语言 > 详细

C++模板使用介绍

时间:2015-04-28 17:58:51      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

http://blog.sina.com.cn/s/blog_6e51df7f01015flt.html

 

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 (c>y)? x:y ;}

但如果在主函数中,我们分别定义了 char a,b; 那么在执行max(a,b);时程序就会出错,因为我们没有定义char类型的重载版本。

现在,我们再重新审视上述的max()函数,它们都具有同样的功能,即求两个数的最大值,能否只写一套代码解决这个问题呢?这样就会避免因重载函数定义不全面而带来的调用错误。为解决上述问题C++引入模板机制,模板定义:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。

2.   函数模板的写法

函数模板的一般形式如下:

Template 或者也可以用typename T>

返回类型 函数名(形参表)
{//函数定义体 }

说明: template是一个声明模板的关键字,表示声明一个模板关键字class不能省略,如果类型形参多余一个,每个形参前都要加class <</span>类型 形参表>可以包含基本数据类型可以包含类类型.

 

请看以下程序:

//Test.cpp

#include 

using std::cout;

using std::endl;

 

//声明一个函数模版,用来比较输入的两个相同数据类型的参数的大小,class也可以被typename代替,

//T可以被任何字母或者数字代替。

template <<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">class T>

T min(T x,T y)

{ return(x

 

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");

}

程序运行结果: 

 

程序分析:main()函数中定义了两个整型变量n1 , n2 两个双精度类型变量d1 , d2然后调用min( n1, n2); 即实例化函数模板T min(T x, T y)其中T为int型,求出n1,n2中的最小值.同理调用min(d1,d2)时,求出d1,d2中的最小值.

3. 类模板的写法

定义一个类模板:

Template < class或者也可以用typename T >
class类名{
//类定义......
};

说明:其中,template是声明各模板的关键字,表示声明一个模板,模板参数可以是一个,也可以是多个。

例如:定义一个类模板:

// ClassTemplate.h
#ifndef ClassTemplate_HH

#define ClassTemplate_HH

 

template<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">typename T1,typename T2>

class myClass{

private:

     T1 I;

     T2 J;

public:

     myClass(T1 a, T2 b);//Constructor

     void show();

};

 

//这是构造函数

//注意这些格式

template <<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">typename T1,typename T2>

myClass::myClass(T1 a,T2 b):I(a),J(b){}

 

//这是void show();

template <<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">typename T1,typename T2>

void myClass::show()

{

     cout<<"I="<<I<<", J="<<J<<endl;

}

#endif

 

// Test.cpp

#include 

#include "ClassTemplate.h"

using std::cout;

using std::endl;

 

void main()

{

     myClass<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">int,int> class1(3,5);

     class1.show();

 

     myClass<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">int,char> class2(3,"a");

     class2.show();

 

     myClass<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">double,int> class3(2.9,10);

     class3.show();

 

     system("PAUSE");

}

 

最后结果显示:

 

 

4.非类型模版参数

一般来说,非类型模板参数可以是常整数(包括枚举)或者指向外部链接对象的指针。

那么就是说,浮点数是不行的,指向内部链接对象的指针是不行的。


template<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">typename T, int MAXSIZE>

 

class Stack{

 

Private:

 

       T elems[MAXSIZE];

 

 

};

 

 

 

Int main()

 

{

 

       Stack<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">int, 20> int20Stack;

 

       Stack<<span style="margin: 0px auto; padding: 0px; border: 0px; list-style-type: none; color: blue;">int, 40> int40Stack;

 

 

};

 

技术分享

 

技术分享

C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们在这篇文章中为大家详细讲解一下有关C++模板的基本概念,希望初学者们可以通过本文介绍的内容充分掌握这方面的知识。

前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:

下面列出了C++模板的常用情况:

1. C++模板类静态成员

 
  1. template < typename T> struct testClass   
  2. {   
  3. static int _data;   
  4. };   
  5. template< > int testClass< char>::_data = 1;   
  6. template< > int testClass< long>::_data = 2;   
  7. int main( void ) {   
  8. cout < <  boolalpha < <  (1==testClass< char>::_data) < <  endl;   
  9. cout < <  boolalpha < <  (2==testClass< long>::_data) < <  endl;   
  10. }  

2. C++模板类偏特化

 
  1. template < class I, class O> struct testClass   
  2. {   
  3. testClass() { cout < <  "I, O" < <  endl; }   
  4. };   
  5. template < class T> struct testClass< T*, T*>   
  6. {   
  7. testClass() { cout < <  "T*, T*" < <  endl; }   
  8. };   
  9. template < class T> struct testClass< const T*, T*>   
  10. {   
  11. testClass() { cout < <  "const T*, T*" < <  endl; }   
  12. };   
  13. int main( void )   
  14. {   
  15. testClass< int, char> obj1;   
  16. testClass< int*, int*> obj2;   
  17. testClass< const int*, int*> obj3;   

3.类模版+函数模版

 
  1. template < class T> struct testClass   
  2. {   
  3. void swap( testClass< T>& ) { cout < <  "swap()" < <  endl; }   
  4. };   
  5. template < class T> inline void swap( testClass< T>& x, 
    testClass< T>& y )   
  6. {   
  7. x.swap( y );   
  8. }   
  9. int main( void )  
  10. {   
  11. testClass< int> obj1;   
  12. testClass< int> obj2;   
  13. swap( obj1, obj2 );   

4. 类成员函数模板

 
  1. struct testClass  
  2. {   
  3. template <</span> class T> void mfun( const T& t )  
  4. {   
  5. cout <</span> <</span>  t <</span> <</span>  endl;   
  6. }   
  7. template <</span> class T> operator T()   
  8. {   
  9. return T();   
  10. }   
  11. };   
  12. int main( void )   
  13. {   
  14. testClass obj;   
  15. obj.mfun( 1 );   
  16. int i = obj;   
  17. cout <</span> <</span>  i <</span> <</span>  endl;   

5. 缺省C++模板参数推导

 
  1. template <</span> class T> struct test   
  2. {   
  3. T a;   
  4. };   
  5. template <</span> class I, class O=test<</span> I> > struct testClass   
  6. {   
  7. I b;   
  8. O c;   
  9. };   
  10. void main()  
  11. {  

6. 非类型C++模板参数

 
  1. template <</span> class T, int n> struct testClass {   
  2. T _t;   
  3. testClass() : _t(n) {   
  4. }   
  5. };   
  6. int main( void ) {   
  7. testClass<</span> int,1> obj1;   
  8. testClass<</span> int,2> obj2;   

7. 空模板参数

 
  1. template <</span> class T> struct testClass;   
  2. template <</span> class T> bool operator==( const testClass<</span> T>&, 
    const testClass<</span> T>& )   
  3. {   
  4. return false;   
  5. };   
  6. template <</span> class T> struct testClass   
  7. {   
  8. friend bool operator== <</span> >
    ( const testClass&, const testClass& );   
  9. };   
  10. void main()  
  11. {  

8. template template 类

 
  1. struct Widget1   
  2. {   
  3. template<</span> typename T>   
  4. T foo(){}   
  5. };   
  6. template<</span> template<</span> class T>class X>   
  7. struct Widget2  
  8. {   
  9. };   
  10. void main()  
  11. {  
  12. cout<</span> <</span>  3 <</span> <</span>  ‘\n‘;  

以上就是对C++模板的一些常用方法的介绍。

C++模板使用介绍

标签:

原文地址:http://www.cnblogs.com/dps001/p/4463308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!