c++中模板是什么?為什么要定義模板?


一、c++中模板是什么?

首先:

1 int Max(int x, int y)
2 {
3     return x > y ? x : y;
4 }
5 
6 float Max(float a,float b)
7 {
8     return a > b ? a : b;
9 }

通常我們想要比較不同數據類型的時候不得不定義兩種不同的函數來表示區分,為了能精簡代碼和避免強類型的嚴格性和靈活性的沖突,我們就需要用到模板去改善這種情況。

二、為什么要定義模板?

強類型程序設計中,參與運算的所有對象的類型在編譯時即確定下來,並且編譯程序將進行嚴格的類型檢查。為了解決強類型的嚴格性和靈活性的沖突。有以下3種方式解決:

1)帶參數宏定義(原樣替換)

2)重載函數(函數名相同,函數參數不同)

3)模板(將數據類型作為參數)

 

模板的使用中函數模板類模板用的最廣最繁。

函數模板

定義:

template <模板參數表>

返回類型 函數名 (參數列表)

{

  //函數體

}

注:class或typename修飾的類型參數,代表一種類型;非類型參數表達式,可以是int,long,long long類型,使用已知類型符,代表一個常量

 1 //1.函數模版的隱式實例化
 2 #include <iostream>
 3 using namespace std;
 4 
 5 template <class T>
 6 T Max(T x, T y);    //函數模版的申明
 7 
 8 int main()
 9 {
10     int intX = 1, intY = 2;
11     double dblX = 3.9, dblY = 2.9;
12     cout << Max(intX, intY) << endl;    //實參為int型,生成int型模板函數,並對第二個參數進行檢查
13     //或者cout << Max<int>(intX, intY) << endl;
14     cout << Max(dblX, dblY) << endl;    //實參為double型,生成double型模板函數,並對第二個參數進行檢查
15     //或者cout << Max<double>(dblX, dblY) << endl;
16     cout << Max(dblY,intX) << endl; //模板函數做不到兩個參數類型不一致還可以比較
17 
18     return 0;
19 }
20 
21 template <class T>
22 T Max(T x, T y)        //函數模版的實現
23 {
24     return (x > y ? x : y);
25 }
 1 //2.函數模板和函數模板的重載
 2 #include <iostream>
 3 using namespace std;
 4 
 5 template < class T >
 6 T Max(T x, T y);
 7 
 8 template <class T>
 9 T Max(T x, T y, T z)
10 {
11     return x > y ? (x > z ? x : z) : (y > z ? y : z);
12 }
13 
14 int main()
15 {
16     int intX = 1, intY = 2, intZ = 3;
17     double dblX = 3.0, dblY = 2.9;
18 
19     cout << Max<int>(intX, intY) << endl;    //調用實例化的Max(int,int)
20     cout << Max<int>(intX, intY, intZ) << endl;    //調用實例化的Max(int,int,int)
21     cout << Max<double>(dblX, dblY) << endl;    //顯示實例化為double型,生成double型模板函數
22     cout << Max('A', '8') << endl;            //隱式實例化char型,生成char型模板函數
23     return 0;
24 }
25 
26 template <class T>
27 T Max(T x, T y)
28 {
29     return x > y ? x : y;
30 }

 

類模板

定義:

template<模板參數表>

class 類名

{

}

下面給出一個棧的模板實現類:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 #define MaxSize 10
  5 
  6 template <class T>
  7 class CStack
  8 {
  9 private:
 10     T data[MaxSize];
 11     int top;
 12 public:
 13     CStack():top(-1)
 14     {
 15     }
 16     void Push(void);
 17     void Pop(void);
 18     bool ifEmpty()
 19     {
 20         if(top == -1)
 21             return true;
 22         else
 23             return false;
 24     }
 25     bool ifFull()
 26     {
 27         if(top == MaxSize-1)
 28             return true;
 29         else
 30             return false;
 31     }
 32     T getTop(void)
 33     {
 34         if(ifEmpty())
 35         {
 36             cout<<"棧為空,不能取棧頂!"<<endl;
 37             return -1;
 38         }
 39         return this->data[top];
 40     }
 41 };
 42 
 43 template <class T>
 44 void CStack<T>::Push(void)
 45 {
 46     if(ifFull())
 47     {
 48         cout<<"棧已滿,不能入棧!"<<endl;
 49         return ;
 50     }
 51     T a;
 52     cin>>a;
 53     this->data[++top] = a;
 54     cout<<"元素"<<a<<"入棧!"<<endl;
 55 }
 56 
 57 template <class T>
 58 void CStack<T>::Pop(void)
 59 {
 60     if(ifEmpty())
 61     {
 62         cout<<"棧為空,不能出棧!"<<endl;
 63         return ;
 64     }
 65     T temp = this->data[top--];
 66     cout<<"元素"<<temp<<"出棧!"<<endl;
 67 }
 68 
 69 
 70 int main()
 71 {
 72     CStack<int> s1;    //可以自己更換數據類型int
 73     int i;
 74     do
 75     {
 76         cout<<"\t===============================\n";
 77         cout<<"\t*********順序棧類模板**********\n";
 78         cout<<"\t           1.入棧              \n";
 79         cout<<"\t           2.出棧              \n";
 80         cout<<"\t           3.取棧頂            \n";
 81         cout<<"\t           0.退出              \n";
 82         cout<<"\t*******************************\n";
 83         cout<<"\t===============================\n";
 84         do
 85         {
 86             cout<<"\tplease input your operator:";
 87             cin>>i;
 88             system("cls");
 89         }while(i!=1 && i!=2 && i!=3 && i!=0);
 90         switch(i)
 91         {
 92         case 1:
 93             s1.Push();
 94             system("pause");
 95             system("cls");
 96             break;
 97         case 2:
 98             s1.Pop();
 99             system("pause");
100             system("cls");
101             break;
102         case 3:
103             if(-1 == s1.getTop())
104             {
105                 system("pause");
106                 system("cls");
107                 break;
108             }
109             else
110                 cout<<"棧頂元素為:"<<s1.getTop()<<endl;
111             system("pause");
112             system("cls");
113             break;
114         }
115     }while(i != 0);
116 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM