C++笔记 --- 模板

 

目录

类模板
函数模板

(本章节中例子都是用 VS2005 编译调试的) 

模版特化:

模板的“特化”(实例化),它发生在编译期,无论一个模板被实例化多少次,都不会影响最终结果,但是这会浪费编译的时间.不知道隐式特化是啥东西.但是显式特化的意思是:当一类东西中出了一渣滓的时候,为了对外接口的统一,或者说是为了家丑不可外扬,有必要把它单独拿出来写一下,然后使他可以和这个类中的所有东西步伐一致.为了需要,针对特定的类型,需要对模板进行特化,也就是特殊处理, 是为模板的特化.

 


 

 

类模板

说明:

类模板就是建立一个通用类,其数据成员的类型、成员函数的返回类型和参数类型都不具体指定,用一个虚拟类型来代表.当使用类模板建立对象时,系统会根据实参的类型来取代类模板中的虚拟类型从而实现不同类的功能

模板参数:

对于函数模板和类模板,模板参数并不局限于类型,普通值也可以作为模板参数.在基于类型参数的模板中,你定义了一些具体细节未加确定的代码,直到代 码被调用时这些细节才被真正确定.然而,在这里,我们面对的这些细节是值(value),而不是类型.当要使用基于值的模板时,你必须显式地指定这些值, 才能够对模板进行实例化,并获得最终代码.

定义格式:

格式一:
  template <typename 类型参数1,typename 类型参数2, ... ,参数类型 参数1,参数类型 参数2, ...>
  class 类名
  {
    类成员声明
  };

格式二:
  template <class 类型参数1,class 类型参数2, ... ,参数类型 参数1,参数类型 参数2, ...>
  class 类名
  {
    类成员声明
  };

类成员声明:

要求

在类成员声明里,成员数据类型、成员函数的返回类型和参数类型前面需加上类型参数.在类模板中成员函数既可以定义在类模板内,也可以定义在类模板外,在外部定义类模板的成员函数外时 C++ 有这样的规定:需要在成员函数定义之前进行模板声明,且在成员函数名之前加上“类名<类型参数>::

格式

对应格式一:
  template <typename 类型参数1,typename 类型参数2, ... ,参数类型 参数1,参数类型 形参2, ...>
  返回类型 类名<类型参数1,参数类型2, ... ,形参1,形参2, ...>::函数名(形参)
  {
    函数体
  }

对应格式二:
  template <class 类型参数1,class 类型参数2, ...  ... ,参数类型 形参1,参数类型 形参2, ...>
  返回类型 类名<类型参数1,参数类型2, ... ,形参1,形参2, ...>::函数名(形参)
  {
    函数体
  }

实例对象声明格式:

  • 类模板名<实际类型1,实际参数2, ... ,实参1,实参2, ...> 对象名;
  • 类模板名<实际类型1,实际参数2, ... ,实参1,实参2, ...> 对象名(构造函数实参);

注意:

  • 在一个类模板的各个实例之间没有联系,形成一个个独立的类
  • 模板的实例可用在一般类可以用的任何地方,用模板类实例定义的对象和一般的对象用法完全相同
  • 实例化的时机:在需要时实例化,比如定义指针或者引用时不需要实例化,定义具体的变量或者常量时不会实例化,而访问对象的成员时会实例化

例子: 

类模板(在类内部声明成员函数):

View Code
 1 // demo.h 文件内容 *******************************
 2 #include<iostream>
 3 using namespace std;
 4 
 5 template<class type>
 6 class A
 7 {
 8 private:
 9     type a;
10     type b;
11 public:
12     A(type x,type y)
13     {
14         a=x;
15         b=y;
16         cout<<"new instance"<<endl;
17         cout<<"the value of a is "<<a<<endl;
18         cout<<"the value of b is "<<b<<endl<<endl;
19     }
20 };
21 // main.cpp 文件内容 *******************************
22 #include"demo.h"
23 
24 void main()
25 {
26     int x=3,y=6;
27     double a=9.45,b=10.23;
28     A<int> app1(x,y);
29     A<double> app2(a,b);
30     system("pause");
31 }
32 /*****************************************
33 输出结果:
34 new instance
35 the value of a is 3
36 the value of b is 6
37 
38 new instance
39 the value of a is 9.45
40 the value of b is 10.23
41 
42 请按任意键继续. . .
43 *****************************************/

类模板(在类外部声明成员函数,成员函数只能在 .h 文件中定义,要是在 .cpp 中定义编译时候会出现链接错误):

View Code
 1 // demo.h 文件内容 *******************************
 2 #include<iostream>
 3 using namespace std;
 4 
 5 template<class type>
 6 class A
 7 {
 8 private:
 9     type a;
10     type b;
11 public:
12     A(type x,type y);
13     void max();
14 };
15 template<class type>
16 A<type>::A(type x,type y)
17 {
18     a=x;
19     b=y;
20     cout<<"new instance"<<endl;
21     cout<<"the value of a is "<<a<<endl;
22     cout<<"the value of b is "<<b<<endl;
23 }  
24 template<class type>
25 void A<type>::max()
26 {
27     cout<<a<<">"<<b<<"?    "<<(a>b?"yes":"no")<<endl;
28 } 
29 // main.cpp 文件内容 *******************************
30 #include"demo.h"
31 
32 void main()
33 {
34     int x=3,y=6;
35     double a=9.45,b=10.23;
36     A<int> app1(x,y);
37     app1.max();
38     cout<<endl;
39     A<double> app2(a,b);
40     app2.max();
41     system("pause");
42 }
43 /******************************************
44 输出结果:
45 new instance
46 the value of a is 3
47 the value of b is 6
48 3>6?    no
49 
50 new instance
51 the value of a is 9.45
52 the value of b is 10.23
53 9.45>10.23?    no
54 请按任意键继续. . .
55 ******************************************/

以上都是用关键字 class 现在举个用 typename 关键字的例子但是其与 class 关键字没有什么差别:

View Code
 1 // demo.h 文件内容 *******************************
 2 #include<iostream>
 3 using namespace std;
 4 
 5 template<typename type>
 6 class A
 7 {
 8 private:
 9     type a;
10     type b;
11 public:
12     A(type x,type y)
13     {
14         a=x;
15         b=y;
16         cout<<"new instance"<<endl;
17         cout<<"the value of a is "<<a<<endl;
18         cout<<"the value of b is "<<b<<endl<<endl;
19     }
20 };
21  // main.cpp 文件内容 *******************************
22 #include"demon.h"
23 
24 void main()
25 {
26     int x=3,y=6;
27     double a=9.45,b=10.23;
28     A<int> app1(x,y);
29     A<double> app2(a,b);
30     system("pause");
31 }
32 /*****************************************
33  输出结果:
34 new instance
35 the value of a is 3
36 the value of b is 6
37 
38 new instance
39 the value of a is 9.45
40 the value of b is 10.23
41 
42 请按任意键继续. . .
43 *****************************************/

带有默认类型参数的类模板:

View Code
 1 // demo.h 的文件内容 *****************************
 2  #include<iostream>
 3  using namespace std;
 4  
 5  template<class type = int> // type 的默认类型参数为 int;
 6  class A
 7  {
 8  private:
 9      type a;
10      type b;
11  public:
12      A(type x,type y);
13  };
14  template<class type>
15  A<type>::A(type x,type y)
16  {
17      a=x;
18      b=y;
19      cout<<"new instance"<<endl;
20      cout<<"the value of a is "<<a<<endl;
21      cout<<"the value of b is "<<b<<endl;
22  }   
23 // main.cpp 的文件内容 ****************************
24  #include"demo.h"
25  
26  void main()
27  {
28      int x=3,y=6;
29      double a=9.45,b=10.23;
30      A<> app1(x,y);
31      cout<<endl;
32      A<double> app2(a,b);
33      system("pause");
34  }
35 /*******************************************
36  输出结果:
37  new instance
38  the value of a is 3
39  the value of b is 6
40  
41  new instance
42  the value of a is 9.45
43  the value of b is 10.23
44  请按任意键继续. . .
45 *******************************************/

带有参数的类模板:

View Code
 1 // demo.h 文件内容 *******************************
 2 #include<iostream>
 3 using namespace std;
 4 
 5 template<class type,int a_x = 0>
 6 class A
 7 {
 8 private:
 9     type a;
10     type b;
11     int c;
12 public:
13     A(type x,type y);
14     void max();
15 };
16 template<class type,int a_x>
17 A<type,a_x>::A(type x,type y)
18 {
19     a=x;
20     b=y;
21     c = a_x;
22     cout<<"new instance"<<endl;
23     cout<<"the value of a is "<<a<<endl;
24     cout<<"the value of b is "<<b<<endl;
25     cout<<"the value of c is "<<c<<endl;
26 }  
27 // main.cpp 文件内容 ******************************
28 #include"demo.h"
29 
30 void main()
31 {
32     int x=3,y=6;
33     double a=9.45,b=10.23;
34     A<int,5> app1(x,y);
35     cout<<endl;
36     A<double,10> app2(a,b);
37     system("pause");
38 }
39 /*******************************************
40 输出结果:
41 new instance
42 the value of a is 3
43 the value of b is 6
44 the value of c is 5
45 
46 new instance
47 the value of a is 9.45
48 the value of b is 10.23
49 the value of c is 10
50 请按任意键继续. . .
51 *******************************************/

友元类模板:

View Code
 1 #include <iostream>
 2 #include <cstdlib>
 3 using  namespace std;
 4 
 5 template<class T2>
 6 class B;   //友元类的向前声明
 7 template<class T1>
 8 class A    //拥有友元类的类的定义
 9 {
10 private:
11     T1 y;
12 public:
13     A(T1 a):y(a){}
14     template<class T2>
15     friend class B;
16 };
17 template<class T2>
18 class B  //友元类的定义
19 {
20 private:
21     T2 x;
22 public:
23     B(T2 a):x(a){};
24     template<class U>
25     void show(A<U> &a)
26     //这里的模板类型参数名可以与模板类 A 定义时候使用的模板参数名不一样
27     {
28         cout<<"the value of A::y is "<<a.y<<endl;
29         cout<<"the value of B::x is "<<x<<endl;
30     }
31 };
32 void main()
33 {
34     A<int> a(5);
35     B<double> b(10.20);
36     b.show(a);
37     system("pause");
38 }
39 /******************************************
40 输出结果为:
41 the value of A::y is 5
42 the value of B::x is 10.2
43 请按任意键继续. . .
44 ******************************************/

 [返回目录]


 

函数模板

说明:

函数模板就是建立一个通用的函数,其参数类型和返回类型不具体指定,用一个虚拟的类型来代表

声明格式:

格式一:
  template<typename 类型参数1,typename 类型参数2, ... >
  返回类型 函数名(模板形参表)
  {
    函数体
  }

格式二:
  template<class 类型参数1,class 类型参数2, ... >
  返回类型 函数名(模板形参表)
  {
    函数体
  }

说明:

template 是一个声明模板的关键字,类型参数一般用T这样的标识符来代表一个虚拟的类型,当使用函数模板时,会将类型参数具体化. typename 和 class 关键字作用都是用来表示它们之后的参数是一个类型的参数.只不过 class 是早期 C++ 版本中所使用的,后来为了不与类产生混淆,所以增加个关键字 typename

注意:

  • 函数模板也可被声明为 inline 或 extern ,但把指示符放在模板参数后
  • 在函数模板定义中声明的对象或类型不能与模板参数同名
  • 模板类型参数名可以被用来指定函数模板的返回值
  • 模板参数名在同一模板参数表只能使用一次,但在多个函数模板声明定义可使用多次
  • 在定义函数模板时要注意的一点是在 template 语句和函数模板定义语句之间是不允许插入其他的语句的,函数模板也可以重载
  • 当模板函数和同名的非模板函数重载时,首先寻找与参数类型完全匹配的非模板函数,找到了,则调用它,如果没找到,则寻找函数模板,找到后具体化函数模板,而后调用该模板函数

模板函数与函数重载:

模板函数类似于重载函数,但两者有很大区别:函数重载时,每个函数体内可以执行不同的动作,但同一个函数模板实例化后的模板函数都必须执行相同的动作

工作原理:

在函数模板被调用时,编译器根据实际参数的类型确定模板参数T的类型,并自动生成一个对应的函数,即模板函数.模板参数的类型不同,生成的模板函数也不同.

例子:

函数模板:

View Code
 1 #include<iostream>
 2 using namespace std;
 3 
 4 //函数模板声明
 5 template<class T,class T2>
 6 void output(T x,T2 y);
 7 //主函数定义
 8 void main()
 9 {
10     double a = 7.09;
11     int b=5;
12     output(a,b);
13     output(b,a);
14     system("pause");
15 
16 }
17 //函数模板定义
18 template<class T,class T2>
19 void output(T x,T2 y)
20 {
21     cout<<x<<"  "<<y<<endl;
22 } 
23 /************************************
24 输出结果:
25 7.09       5
26 5       7.09
27 请按任意键继续. . .
28 ************************************/

模板函数和同名的非模板函数重载:

View Code
 1 #include<iostream>
 2 
 3 template<class T>
 4 void max(T x,T y)
 5 {
 6     std::cout<<x<<">"<<y<<"?      "<<(x>y?"yes":"no")<<std::endl;
 7 } 
 8 void max(int x,int y)
 9 {
10     std::cout<<x<<">"<<y<<"?      "<<(x>y?"yes":"no")<<std::endl;
11 }
12 void main()
13 {
14     double a = 7.09 , b = 10.22;
15     long c = 8,d = 9;
16     int e = 10,f = 5;
17     max(a,b); //调用的是重载函数模板 void max(T x,T y)
18     max(c,d); //调用的是重载函数模板 void max(T x,T y)
19     max(e,f); //调用的是重载函数 void max(int x,int y)
20     system("pause");
21 }
22 /***************************************
23 输出结果:
24 7.09>10.22?      no
25 8>9?      no
26 10>5?      yes
27 请按任意键继续. . .
28 ***************************************/

在函数外部友元函数模板:

View Code
 1 #include <iostream>  
 2 using namespace std;  
 3 
 4 template<class Type> class A
 5 {
 6 public:
 7     A(Type b):a(b){}
 8     template<class Type>
 9     friend void output(const A<Type>& b); 
10     Type a;  
11 }; 
12 
13 //在类外部定义友元函数   
14 template<class Type>
15 void output(const A<Type>& b)  
16 {
17     cout<<b.a<<endl;
18 }  
19 
20 int main(void)  
21 {  
22     A<int>  a(10);       
23     output(a);  
24     system("pause");  
25 }
26 // 输出结果为: 10

在函数内部友元函数模板:

View Code
 1 #include <iostream>  
 2 using namespace std;  
 3 
 4 template<class Type> class A
 5 {
 6 public:
 7     A(Type b):a(b){}
 8     //在类内部定义友元函数    
 9     friend void output(const A& a){cout<<a.a<<endl;}
10     /******************************************************
11     等价形式:        
12     template<class T>friend void output(const A<T>& a){cout<<a.a<<endl;}
13     这里的模板参数名可以与类声明中模板参数变量名不一样
14     ******************************************************/
15     Type a;  
16 }; 
17 
18 int main(void)  
19 {  
20     A<int>  a(10);       
21     output(a);  
22     system("pause");  
23 }
24 // 输出结果为: 10

模板友元的另一种声明格式(向前声明):

View Code
 1 #include <iostream>  
 2 using namespace std;  
 3 
 4 template<class Type> class A; //向前声明
 5 template<class Type> void output(const A<Type>& b); //友元函数声明
 6 /******************************************
 7 //当然也可以是定义:
 8 template<class Type>
 9 void output(const A<Type>& b)  
10 {
11     cout<<b.a<<endl;
12 } 
13 ******************************************/
14 template<class Type> class A
15 {
16 public:
17     A(Type b):a(b){}
18     friend void output<>(const A& b);
19     /******************************************
20     上面声明的等价形式:
21     friend void output<>(const A<Type>& b);
22     friend void output<Type>(const A<Type>& b); 
23     ******************************************/
24     Type a;  
25 }; 
26 
27 //在类外部定义友元函数   
28 template<class Type>
29 void output(const A<Type>& b)  
30 {
31     cout<<b.a<<endl;
32 } 
33 
34 int main(void)  
35 {  
36     A<int>  a(10);       
37     output(a);  
38     system("pause");  
39 }
40 // 输出结果为: 10

[返回目录]

posted @ 2012-07-02 19:27  kzangv  阅读(955)  评论(0编辑  收藏  举报
top