淺析C++中模板的那點事
1.什么是模板
假設現(xiàn)在我們完成這樣的函數(shù),給定兩個數(shù)x和y求式子x^2 + y^2 + x * y的值 .考慮到x和y可能是 int , float 或者double類型,那么我們就要完成三個函數(shù):
int fun(int x,int y);
float fun(float x,float y);
double fun(double x,double y);
并且每個fun函數(shù)內(nèi)部所要完成的操作也是極其的相似。如下:
int fun(int x,int y)
{
int tmp = x *x + y * y + x * y;
return tmp;
}
float fun(float x,float y)
{
float tmp = x *x + y * y + x * y;
return tmp;
}
double fun(double x,double y)
{
double tmp = x *x + y * y + x * y;
return tmp;
}
可以看出,上面的三個函數(shù)體除了類型不一樣之外,其他的完全一樣,那么如果能夠只寫一個函數(shù)就能完成上面的三個函數(shù)的功能該多好呢?如果從這三個函數(shù)提煉出一個通用函數(shù),而它又適用于這三種不同類型的數(shù)據(jù),這樣會使代碼的重用率大大提高。實際上C++中的模板正好就是來解決這個問題的。模板可以實現(xiàn)類型的參數(shù)化(把類型定義為參數(shù)),從而實現(xiàn)了真正的代碼可重用性。C++中的模板可分為函數(shù)模板和類模板,而把函數(shù)模板的具體化稱為模板函數(shù),把類模板的具體化成為模板類。下面讓我們分別看看什么是函數(shù)模板和類模板吧~~~
2.模板函數(shù)
實際上我們利用函數(shù)模板,只需要一個函數(shù)就可能完成上面的三個函數(shù)了,千言萬語不如看代碼:
#include <iostream>
using namespace std;
template <typename T>
T fun(T x,T y)
{
T tmp = x *x + y * y + x * y;
return tmp;
}
int main()
{
int x1 = 1,y1 = 4;
float x2 = 1.1 , y2 = 2.2;
double x3 = 2.0 , y3 = 3.1;
cout<<fun(x1,y1)<<endl;
cout<<fun(x2,y2)<<endl;
cout<<fun(x3,y3)<<endl;
return 0;
}
運行結果:
如此利用模板,我們很輕而易舉的達到了我們的目的,而這也大大的提高了代碼的可重用性,這也讓我們想起了STL中的那些算法了吧,這些算法使用多種的數(shù)據(jù)類型。實際上STL即使模板的重要應用了。
現(xiàn)在我們想,如果上面的代碼這樣調(diào)用fun(x1,y2)會怎么樣呢?點擊編譯會出現(xiàn)這樣的錯誤:
可以看到編譯編譯出現(xiàn)問題的是fun(x1,y2),說的意思就是沒有對應的函數(shù),要么x1和y2都是int型,要么x1和y2都是float型。那么我為什么要說一下這樣一種情況呢?主要是為了引出模板也可以同時使用兩個:
#include <iostream>
using namespace std;
template <typename T1 , typename T2>
T2 fun(T1 x,T2 y)
{
T2 tmp = x *x + y * y + x * y;
return tmp;
}
int main()
{
int x1 = 1,y1 = 4;
float x2 = 1.1 , y2 = 2.2;
double x3 = 2.0 , y3 = 3.1;
cout<<fun(x1,y1)<<endl;
cout<<fun(x2,y2)<<endl;
cout<<fun(x3,y3)<<endl;
cout<<fun(x1,y2)<<endl;
return 0;
}
運行結果:
當使用兩個模板時,為什么fun(x1,y1)也能正確運行呢?因為當進行這個調(diào)用時,T1 = int ,T2 = int。所以這種調(diào)用也是沒有問題的。
提到函數(shù)想到重載是很自然的吧,那么模板函數(shù)能不能重載呢?顯然是能的了,還是看代碼:
#include <iostream>
using namespace std;
template <typename T1 , typename T2>
T2 fun(T1 x,T2 y)
{
cout<<"調(diào)用了兩個個參數(shù)的 fun 函數(shù) ^^ "<<endl;
T2 tmp = x *x + y * y + x * y;
return tmp;
}
template <typename T>
T fun(T x , T y , T z)
{
cout<<"調(diào)用了三個參數(shù)的 fun 函數(shù) ^^ "<<endl;
T tmp = x * x + y * y + z * z + x * y * z;
return tmp;
}
int main()
{
int x1 = 1 , y1 = 4 , z1 = 5;
float x2 = 1.1 , y2 = 2.2;
double x3 = 2.0 , y3 = 3.1;
cout<<fun(x1,y1)<<endl;
cout<<fun(x2,y2)<<endl;
cout<<fun(x3,y3)<<endl;
cout<<fun(x1,y2)<<endl;
cout<<fun(x1,y1,z1)<<endl;
return 0;
}
運行結果:
從結果已經(jīng)能看出來模版函數(shù)的重載是沒有任何問題的了。那么模板函數(shù)和非模板函數(shù)之間是否能夠重載呢??
#include <iostream>
using namespace std;
template <typename T>
T fun(T x,T y)
{
cout<<"調(diào)用了模板函數(shù) ^^ "<<endl;
T tmp = x * x + y * y + x * y;
return tmp;
}
int fun(int x,int y)
{
cout<<"調(diào)用了非模板函數(shù) ^^ "<<endl;
int tmp = x * x + y * y + x * y;
return tmp;
}
int main()
{
int x1 = 1 , y1 = 4;
float x2 = 1.1 , y2 = 2.2;
cout<<fun(x1,y1)<<endl;
cout<<fun(x2,y2)<<endl;
return 0;
}
運行結果:
看以看出模版函數(shù)和非模板函數(shù)也是可能重載的,那么重載函數(shù)的調(diào)用順序是怎么樣的呢?實際上是先查找非模板函數(shù),要有嚴格匹配的非模板函數(shù),就調(diào)用非模板函數(shù),找不到適合的非模板函數(shù)在和模板函數(shù)進行匹配。
到這里,關于模板就說這些吧~~~~
3.模板類
要是理解了模版函數(shù),模板類就相當?shù)暮唵瘟?,只不過模版函數(shù)是對函數(shù)中的類型使用模板,而模板類是對類中的類型使用模板,這我就不多說了,下面的代碼是我以前利用模板寫的單鏈表,這個是模板的典型應用:(測試過)
#include <stdio.h>
#include <iostream.h>
template <class T>
struct SLNode
{
T data;
SLNode<T> *next;
SLNode(SLNode<T> *nextNode=NULL)
{
next = nextNode;
}
SLNode(const T &item,SLNode<T> *nextNode=NULL)
{
data = item;
next = nextNode;
}
};
template <class T>
class SLList
{
private:
SLNode<T> *head;
SLNode<T> *tail;
SLNode<T> *currptr;
int size;
public:
SLList();
SLList(const T &item);
~SLList();
bool IsEmpty()const;
int Length()const;
bool Find(int k,T &item)const;
int Search(const T &item)const;
void InsertFromHead(const T &item);
void InsertFromTail(const T &item);
bool DeleteFromHead(T &item);
bool DeleteFromTail(T &item);
void Insert(int k,const T &item);
void Delete(int k,T &item);
void ShowListMember();
};
//構造函數(shù)
template <class T>
SLList<T>::SLList()
{
head = tail = currptr = new SLNode<T>();
size = 0;
}
//構造函數(shù)
template <class T>
SLList<T>::SLList(const T &item)
{
tail = currptr = new SLNode<T>(item);
head = new SLNode<T>(currptr);
size = 1;
}
//析構函數(shù)
template <class T>
SLList<T>::~SLList()
{
SLNode<T> *temp;
while(!IsEmpty())
{
temp = head->next;
head->next = temp->next;
delete temp;
}
}
//判斷鏈表是否為空
template <class T>
bool SLList<T>::IsEmpty()const
{
return head->next == NULL;
}
//返回鏈表的長度
template <class T>
int SLList<T>::Length()const
{
return size;
}
//查找第k個節(jié)點的閾值
template <class T>
bool SLList<T>::Find(int k,T &item)const
{
if(k < 1)
{
cout<<"illegal position !"<<endl;
}
SLNode<T> *temp = head;
int count = 0;
while(temp != NULL && count < k)
{
temp = temp->next;
count++;
}
if(temp == NULL)
{
cout<<"The list does not contain the K node !"<<endl;
return false;
}
item = temp->data;
return true;
}
//查找data閾值為item是表的第幾個元素
template <class T>
int SLList<T>::Search(const T &item)const
{
SLNode<T> *temp = head->next;
int count = 1;
while(temp != NULL && temp->data != item)
{
temp = temp->next;
count++;
}
if(temp == NULL)
{
cout<<"The node does not exist !"<<endl;
return -1;
}
else
{
return count;
}
}
//從表頭插入
template <class T>
void SLList<T>::InsertFromHead(const T &item)
{
if(IsEmpty())
{
head->next = new SLNode<T>(item,head->next);
tail = head->next;
}
else
{
head->next = new SLNode<T>(item,head->next);
}
size++;
}
//從表尾插入
template <class T>
void SLList<T>::InsertFromTail(const T &item)
{
tail->next = new SLNode<T>(item,NULL);
tail = tail->next;
size++;
}
//從表頭刪除
template <class T>
bool SLList<T>::DeleteFromHead(T &item)
{
if(IsEmpty())
{
cout<<"This is a empty list !"<<endl;
return false;
}
SLNode<T> *temp = head->next;
head->next = temp->next;
size--;
item = temp->data;
if(temp == tail)
{
tail = head;
}
delete temp;
return true;
}
//從表尾刪除
template <class T>
bool SLList<T>::DeleteFromTail(T &item)
{
if(IsEmpty())
{
cout<<"This is a empty list !"<<endl;
return false;
}
SLNode<T> *temp = head;
while(temp->next != tail)
{
temp = temp->next;
}
item = tail->data;
tail = temp;
tail->next=NULL;
temp = temp->next;
delete temp;
size--;
return true;
}
//在第k個節(jié)點后插入item值
template <class T>
void SLList<T>::Insert(int k,const T &item)
{
if(k < 0 || k > size)
{
cout<<"Insert position Illegal !"<<endl;
return;
}
if(k == 0)
{
InsertFromHead(item);
return;
}
if(k == size)
{
InsertFromTail(item);
return;
}
SLNode<T> *temp = head->next;
int count = 1;
while(count < k)
{
count++;
temp = temp->next;
}
SLNode<T> *p = temp->next;
temp->next = new SLNode<T>(item,p);
size++;
}
//刪除第k個節(jié)點的值,保存在item中
template <class T>
void SLList<T>::Delete(int k,T &item)
{
if(k <= 0 || k > size)
{
cout<<"Ileegal delete position !"<<endl;
return;
}
if(k == 1)
{
DeleteFromHead(item);
return;
}
if(k == size)
{
DeleteFromTail(item);
return;
}
SLNode<T> *temp = head->next;
int count = 1;
while(count < k-1)
{
count++;
temp = temp->next;
}
SLNode<T> *p = temp->next;
temp->next = p->next;
p->next = NULL;
item = p->data;
delete p;
size--;
}
template <class T>
void SLList<T>::ShowListMember()
{
cout<<"List Member : ";
SLNode<T> *temp = head->next;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
/*
1.引入了InsertFronHead,InsertFromTail,DeleteFromHead和DeleteFromTail用來實現(xiàn)Insert和Delete函數(shù),是一個比較好的方法。
2.SLNode(T &item,SLNode<T> *nextNode)這個構造函數(shù)設計的非常巧妙,便于其他成員函數(shù)的實現(xiàn)。
3.插入,刪除分為:表頭,表尾,中間插入(刪除)三種情況
*/
int main()
{
int item;
SLList<int> list(12);
list.Insert(0,11);
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
list.Insert(2,14);
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
list.Insert(2,13);
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
list.Delete(2,item);
cout<<"item = "<<item<<endl;
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
list.Delete(1,item);
cout<<"item = "<<item<<endl;
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
list.Delete(2,item);
cout<<"item = "<<item<<endl;
cout<<"list number:"<<list.Length()<<endl;
list.ShowListMember();
return 0;
}
利用模板的好處是,SLList中的數(shù)據(jù)可以是任意的數(shù)據(jù)類型,這也就是泛型編程的概念了吧~~~~