C++笔记------模版
模版-----是為了讓代碼更加通用,使代碼不受數(shù)據(jù)類型的影響。減少代碼冗余。模版將數(shù)據(jù)類型當(dāng)作一個參數(shù)進行傳遞。包括函數(shù)模版和類模板。
函數(shù)模版:
//定義一個比較大小的函數(shù)模版 template<typename Type> // 也可以寫成 template <class Type> Type Max(Type a,Type b) {return a > b ? a : b; } int main() {cout << Max(1,2) << endl;cout << Max('A', 'a') << endl;cout << Max(2.5,3.2) << endl;return 0; }模版會根據(jù)傳遞的實參自動進行數(shù)據(jù)類型的推演,比如在Max(2.5,2.3)中,模版會根據(jù)2.5是double,2.3是double,模版會推導(dǎo)出Type是double類型,生成一個模版函數(shù),使用double類型的比較函數(shù)。所以模版雖然方便,但是效率不高。
比如,調(diào)用函數(shù)Max(1,2)時,編譯器會先根據(jù)函數(shù)模版生成一個int類型的模版函數(shù),然后在調(diào)用這個函數(shù)。
//模版函數(shù)int Max(int a, int b) {return a > b ? a : b }
當(dāng)出現(xiàn)實參類型不一致時,普通函數(shù)正常運行,模版會出現(xiàn)錯誤,如:
/*template<typename Type> //會產(chǎn)生二義性 Type Max(Type a,Type b) {return a > b ? a : b; }
*/ int Max(int a,int b) //會自動進行隱式轉(zhuǎn)換 {return a > b ? a : b; } int main() {cout << Max(1,2.3) << endl;
cout << Max(1,(int)2.3) << endl; //將2.3強制轉(zhuǎn)換成int類型
cout << Max<int>(1,2.3) << endl; //指定調(diào)用int類型的模版函數(shù) }
也可以重新編寫模版函數(shù),如:
template<typename Type1, typename Type2> Type2 Max(Type1 a,Type2 b) {return a > b ? a : b; }如果是類對象進行比較,需要重載比較運算符。模版只負責(zé)比較,不管如何進行比較。
class Test {int num; public:Test(int b):num(b){}bool operator>(const Test & t){if (this->num > t.num)return true;else return false;} }; template<typename Type1> Type1 Max(Type1 a,Type1 b) {return a > b ? a : b; } int main() {Test t1(10);Test t2(9);Max(t1,t2); //不能使用cout輸出,因為沒有提供<<運算符函數(shù) }類模板:
利用類模板簡單實現(xiàn)線性鏈表。
?int a = int(); //將a初始化為0;
模版類成員函數(shù)都是模版函數(shù),不允許將類定義和實現(xiàn)分離
template<typename Type> class ListNode { private:Type data;ListNode<Type> *next; public:friend class List<Type>; //將List類成為ListNode類的友元類,才能訪問私有數(shù)據(jù)ListNode():data(Type()),next(NULL){} //零初始化:根據(jù)不同類型進行初始化。如,int a = int() //a被初始化為0。ListNode(Type d,ListNode<Type> *n = NULL):data(d),next(n){}~ListNode(){} };template<typename Type> class List { private:ListNode<Type> *first;ListNode<Type> *last;size_t size; public:List();~List();bool push_back(Type x); //尾部插入鏈表
//顯示列表函數(shù)void Show_list() const //模版類的成員函數(shù)可以在類內(nèi)部定義{ListNode<Type> *p=first;while(p != NULL){cout << p->data;p = p->next;}} }; template<typename Type> //模版類的成員函數(shù)都是模版函數(shù),所以必須寫template<typename Type> List<Type>::List() //限定是List<Type>:: {first = last = new ListNode<Type>;last->next = NULL;size=0; } template<typename Type> List<Type>::~List() {ListNode<Type> *p=first;while(p != NULL){first = p->next;delete p; //在構(gòu)造函數(shù)中使用new,則在析構(gòu)函數(shù)中使用deletesize--;p=first;} } template<typename Type> bool List<Type>::push_back(Type x) {ListNode<Type> *s = new ListNode<Type>;if( s == NULL )return false;s->data = x;s->next = NULL;last->next = s;last = s;return true; } int main() {List<int> mylist;for(int i=1;i<10;i++){mylist.push_back(i);}mylist.Show_list();return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/zhangzeze/p/8717968.html
總結(jié)
以上是生活随笔為你收集整理的C++笔记------模版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 夏天做梦梦到下雪了是什么意思
- 下一篇: target runtime apach