C++中 模板Template的使用

1、在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?
答:class用于定义类,在模板引入c++后,最初定义模板的方法为:template,这里class关键字表明T是一个类型,后来为了避免class在这两个地方的使用可能给人带来混淆,所以引入了typename这个关键字,它的作用同class一样表明后面的符号为一个类型,这样在定义模板的时候就可以使用下面的方式了: template.在模板定义语法中关键字class与typename的作用完全一样。
2.类模板与模板类的概念
(1) 什么是类模板
一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员、默写成员函数的参数、某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的)。
如果一个类中数据成员的数据类型不能确定,或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的、实际的类,而是代表着一类类。
(2)类模板定义
定义一个类模板,一般有两方面的内容:
A.首先要定义类,其格式为:

template <class T>
class foo
{
……
}

foo 为类名,在类定义体中,如采用通用数据类型的成员,函数参数的前面需加上T,其中通用类型T可以作为普通成员变量的类型,还可以作为const和static成员变量以及成员函数的参数和返回类型之用。template
class Test{
private:
T n;
const T i;
static T cnt;
public:
Test():i(0){}
Test(T k);
~Test(){}
void print();
T operator+(T x);
};在类定义体外定义成员函数时,若此成员函数中有模板参数存在,则除了需要和一般类的体外定义成员函数一样的定义外,还需在函数体外进行模板声明
例如
template
void Test::print(){
std::cout<<”n=”<

80 const short s=3; Ci

.   #include <iostream.h> 
2.  class X  
3.  { 
4.  public:  
5.      void x()  
6.      {   
7.          cout<<"This is fuction x()"<<endl; 
8.      }  
9.  };  
10. class Y  
11. { 
12. public:  
13.     void y()  
14.     {    
15.         cout<<"This is fuction y()"<<endl;  
16.     }  
17. };  
18.   
19. template <typename T> class Z  
20. { 
21.     T t;  
22. public:  
23.     void a() { t.x(); }  
24.     void b() { t.y(); }  
25. };  
26.   
27. int main()  
28. { 
29.     Z<X> zx;  
30.     zx.a(); // Doesn't create Z<X>::b()  
31.     Z<Y> zy;  
32.     zy.b(); // Doesn't create Z<Y>::a() 
33.     return 0; 
34. } 
最后用模板技术演示list的的使用。
1.  // Template class for storing list elements 
2.  #include <iostream> 
3.  #include <string> 
4.  using namespace std; 
5.  template <class T>                          // Use template keyword 
6.  class ListElement                            //定义类ListElement,用于表示list对象 
7.  { 
8.  public: 
9.      T data; 
10.     ListElement<T> * next ; 
11.     ListElement(T& i_d, ListElement <T>* i_n) 
12.     : data(i_d),next(i_n) { } 
13.     ListElement<T>* copy()                    // copy includes all next elements 
14.     {     
15.         return new ListElement(data,(next?next->copy():0));     
16.     } 
17. }; 
18. template <class T>  
19. class ListIterator        //定义类ListIterator,用于访问和操作list对象 
20. {  
21. public : 
22.                                             //ListIterator(List<T>& l) ; 
23.    T operator()() ; 
24.    int operator++() ; 
25.    int operator!() ; 
26. public: 
27.      ListElement<T>* rep ; 
28. }; 
29. template <class T> 
30. T ListIterator<T>::operator() () 
31. {  
32.     if (rep) return rep->data; 
33.     else  
34.     { 
35.         T tmp ;    return tmp ;                // Default value   
36.     } 
37. } 
38. template <class T> 
39. int ListIterator<T>::operator++() 
40. {    
41.     if (rep) 
42.         rep = rep->next ;    
43.     return (rep != 0) ; 
44. } 
45. template <class T> 
46. int ListIterator<T>::operator!() 
47. {    
48.     return (rep != 0) ; 
49. } 
50. template <class T> 
51. class List                //定义类List  
52. { 
53. public: 
54.     friend class ListIterator<T> ; 
55.     List(); 
56.     List(const List&); 
57.     ~List(); 
58.     List<T>& operator=(const List<T>&); 
59.                                             // typical list ops 
60.     T head(); 
61.     List<T> tail(); 
62.     void add(T&); 
63.     friend ostream& operator<<(ostream&, const List<T>&); 
64. public: 
65.     void clear() ;                            // Delete all list elements 
66.     ListElement<T>* rep ; 
67. }; 
68.                                             // Default Constructor 
69. template <class T> 
70. List<T>::List(){ rep = 0 ; } 
71.                                             // Copy Constructor 
72. template <class T>List<T>::List(const List<T>& l) 
73. {      
74.     rep = l.rep ? l.rep->copy() : 0 ; 
75. } 
76.                                             // Overloaded assignment operator 
77. template <class T> 
78. List<T>& List<T>::operator=(const List<T>& l) 
79. { 
80.     if (rep != l.rep)    
81.     {         
82.         clear() ;         
83.         rep = l.rep ? l.rep->copy() : 0 ;     
84.     } 
85.     return *this ; 
86. } 
87.                                             // Destructor 
88. template <class T> 
89. List<T>::~List(){  clear() ;} 
90.                                             // Delete representation 
91. template <class T> 
92. void List<T>::clear() 
93. {   while (rep) 
94.     {   ListElement<T>* tmp = rep ; 
95.         rep = rep->next ; 
96.         delete tmp ; 
97.     } 
98.     rep = 0 ; 
99. } 
100.                                                // Add element to front of list 
101.    template <class T> 
102.    void List<T>::add(T& i) 
103.    {    
104.        rep = new ListElement<T>(i,rep) ; 
105.    } 
106.                                                // Return head of list or default value of type T 
107.    template <class T> 
108.    T List<T>::head() 
109.    {  
110.        if (rep) return rep->data ;  
111.        else  
112.        { 
113.            T tmp ;  
114.            return tmp ; 
115.        } 
116.    } 
117.                                                // Return tail of list or empty list 
118.    template <class T> 
119.    List<T> List<T>::tail() 
120.    {    List<T> tmp ; 
121.         if (rep) 
122.           if (rep->next) tmp.rep = rep->next->copy() ; 
123.         return tmp ; 
124.    } 
125.                                                // Output operator 
126.    template <class T> 
127.    ostream& operator<<(ostream& os, const List<T>& l) 
128.    { 
129.        if (l.rep) 
130.        { 
131.            ListElement<T>* p = l.rep ; 
132.            os << "( " ; 
133.            while (p){ os << p->data << " " ;   p = p->next ; } 
134.            os << ")\n" ; 
135.        } 
136.        else 
137.        os << "Empty list\n" ; 
138.        return os ; 
139.    } 
140.    int main() 
141.    { 
142.       List<int> l ;                            // Integer list 
143.       cout << l ; 
144.       int i=1; 
145.       l.add(i) ; 
146.       i=2; 
147.       l.add(i) ; 
148.       i=3; 
149.       l.add(i) ; 
150.       cout << "l is " << l << endl ; 
151.       cout << "head of l is " << l.head() << endl ; 
152.       List<int> m = l.tail() ; 
153.       List<int> o ; 
154.       o = m; 
155.       i=4; 
156.       m.add(i); 
157.       cout << "m is " << m << endl ; 
158.       cout << "o is " << o << endl ; 
159.       List<char> clist ;                        // Character list 
160.       char ch; 
161.       ch='a'; 
162.       clist.add(ch); 
163.       ch='b'; 
164.       clist.add(ch); 
165.       cout << clist << endl ; 
166.       List<string> ls ;                        // string List 
167.       ls.add(string("hello")) ; 
168.       ls.add(string("world")) ; 
169.       cout << "List of strings" << endl ; 
170.       cout << ls << endl ; 
171.       List<List<int> > listlist ;                // List of lists of integer. Notice that lists of lists are possible 
172.       listlist.add(o) ; 
173.       listlist.add(m) ; 
174.       cout << "List of lists of ints\n" ; 
175.       cout << listlist << endl ; 
176.       List<List<List<int> > > lllist ;            // List of lists of lists of integer 
177.       lllist.add(listlist) ; 
178.       lllist.add(listlist) ; 
179.       cout << "List of lists of lists of ints\n" ; 
180.       cout << lllist << "\n" ; 
181.       List<List<string> > slist ;               // List of list of strings 
182.       slist.add(ls) ; 
183.       slist.add(ls) ; 
184.       cout << "List of lists of strings\n" ; 
185.       cout << slist << "\n" ; 
186.       return 0 ; 
187.    } 

注意:以上某一个实例好像未通过调试,基于时间本人已忘记,读者发现后可查看相关情况自行更正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值