栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

浅析C++中模板的那点事

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅析C++中模板的那点事

1.什么是模板

假设现在我们完成这样的函数,给定两个数x和y求式子x^2 + y^2 + x * y的值 .考虑到x和y可能是 int , float 或者double类型,那么我们就要完成三个函数:

int fun(int x,int y);
float fun(float x,float y);
double fun(double x,double y);

并且每个fun函数内部所要完成的操作也是极其的相似。如下:
复制代码 代码如下:
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;
}

可以看出,上面的三个函数体除了类型不一样之外,其他的完全一样,那么如果能够只写一个函数就能完成上面的三个函数的功能该多好呢?如果从这三个函数提炼出一个通用函数,而它又适用于这三种不同类型的数据,这样会使代码的重用率大大提高。实际上C++中的模板正好就是来解决这个问题的。模板可以实现类型的参数化(把类型定义为参数),从而实现了真正的代码可重用性。C++中的模板可分为函数模板和类模板,而把函数模板的具体化称为模板函数,把类模板的具体化成为模板类。下面让我们分别看看什么是函数模板和类模板吧~~~

2.模板函数

实际上我们利用函数模板,只需要一个函数就可能完成上面的三个函数了,千言万语不如看代码:
复制代码 代码如下:
#include

using namespace std;

template
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<    cout<    cout<    return 0;
}

运行结果:

如此利用模板,我们很轻而易举的达到了我们的目的,而这也大大的提高了代码的可重用性,这也让我们想起了STL中的那些算法了吧,这些算法使用多种的数据类型。实际上STL即使模板的重要应用了。

现在我们想,如果上面的代码这样调用fun(x1,y2)会怎么样呢?点击编译会出现这样的错误:

可以看到编译编译出现问题的是fun(x1,y2),说的意思就是没有对应的函数,要么x1和y2都是int型,要么x1和y2都是float型。那么我为什么要说一下这样一种情况呢?主要是为了引出模板也可以同时使用两个:
复制代码 代码如下:
#include

using namespace std;


template
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<    cout<    cout<    cout<    return 0;
}

运行结果:

当使用两个模板时,为什么fun(x1,y1)也能正确运行呢?因为当进行这个调用时,T1 = int ,T2 = int。所以这种调用也是没有问题的。

提到函数想到重载是很自然的吧,那么模板函数能不能重载呢?显然是能的了,还是看代码:
复制代码 代码如下:
#include

using namespace std;


template
T2 fun(T1 x,T2 y)
{
    cout<<"调用了两个个参数的 fun 函数 ^^ "<    T2 tmp = x *x + y * y + x * y;
    return tmp;
}
template
T fun(T x , T y , T z)
{
    cout<<"调用了三个参数的 fun 函数 ^^ "<    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<    cout<    cout<    cout<    cout<    return 0;
}

运行结果:

从结果已经能看出来模版函数的重载是没有任何问题的了。那么模板函数和非模板函数之间是否能够重载呢??
复制代码 代码如下:
#include

using namespace std;

template
T fun(T x,T y)
{
    cout<<"调用了模板函数 ^^ "<    T tmp = x * x + y * y + x * y;
    return tmp;
}
int fun(int x,int y)
{
    cout<<"调用了非模板函数 ^^ "<    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<    cout<    return 0;
}

运行结果:

看以看出模版函数和非模板函数也是可能重载的,那么重载函数的调用顺序是怎么样的呢?实际上是先查找非模板函数,要有严格匹配的非模板函数,就调用非模板函数,找不到适合的非模板函数在和模板函数进行匹配。

到这里,关于模板就说这些吧~~~~

3.模板类

要是理解了模版函数,模板类就相当的简单了,只不过模版函数是对函数中的类型使用模板,而模板类是对类中的类型使用模板,这我就不多说了,下面的代码是我以前利用模板写的单链表,这个是模板的典型应用:(测试过)
复制代码 代码如下:
#include
#include

template
struct SLNode
{
    T data;
    SLNode *next;
    SLNode(SLNode *nextNode=NULL)
    {
        next = nextNode;
    }
    SLNode(const T &item,SLNode *nextNode=NULL)
    {
        data = item;
        next = nextNode;
    }
};

template
class SLList
{
private:
    SLNode *head;
    SLNode *tail;
    SLNode *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();
};
//构造函数
template
SLList::SLList()
{
    head = tail = currptr = new SLNode();
    size = 0;
}
//构造函数
template
SLList::SLList(const T &item)
{
    tail = currptr = new SLNode(item);
    head = new SLNode(currptr);
    size = 1;
}
//析构函数
template
SLList::~SLList()
{
     SLNode *temp;
    while(!IsEmpty())
    {
        temp = head->next;
        head->next = temp->next;
        delete temp;

    }
}
//判断链表是否为空
template
bool SLList::IsEmpty()const
{
    return head->next == NULL;
}
//返回链表的长度
template
int SLList::Length()const
{
     return size;
}
//查找第k个节点的阈值
template
bool SLList::Find(int k,T &item)const
{
    if(k < 1)
    {
        cout<<"illegal position !"<    }
    SLNode *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 !"<        return false;
    }
    item = temp->data;
    return true;
}
//查找data阈值为item是表的第几个元素
template
int SLList::Search(const T &item)const
{
    SLNode *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 !"<        return -1;
    }
    else
    {
        return count;
    }
}
//从表头插入
template
void SLList::InsertFromHead(const T &item)
{   
    if(IsEmpty())
    {
        head->next = new SLNode(item,head->next);
        tail = head->next;
    }
    else
    {
        head->next = new SLNode(item,head->next);
    }
    size++;
}
//从表尾插入
template
void SLList::InsertFromTail(const T &item)
{
    tail->next = new SLNode(item,NULL);
    tail = tail->next;
    size++;
}
//从表头删除
template
bool SLList::DeleteFromHead(T &item)
{
    if(IsEmpty())
    {
        cout<<"This is a empty list !"<        return false;
    }
    SLNode *temp = head->next;
    head->next = temp->next;
    size--;
    item = temp->data;
    if(temp == tail)
    {
        tail = head;
    }
    delete temp;
    return true;
}
//从表尾删除
template
bool SLList::DeleteFromTail(T &item)
{
    if(IsEmpty())
    {
        cout<<"This is a empty list !"<        return false;
    }
    SLNode *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个节点后插入item值
template
void SLList::Insert(int k,const T &item)
{
    if(k < 0 || k > size)
    {
        cout<<"Insert position Illegal !"<        return;
    }
    if(k == 0)
    {
        InsertFromHead(item);
        return;
    }
    if(k == size)
    {
        InsertFromTail(item);
        return;
    }
    SLNode *temp = head->next;
    int count = 1;
    while(count < k)
    {
        count++;
        temp = temp->next;
    }
    SLNode *p = temp->next;
    temp->next = new SLNode(item,p);
    size++;
}
//删除第k个节点的值,保存在item中
template
void SLList::Delete(int k,T &item)
{
    if(k <= 0 || k > size)
    {
        cout<<"Ileegal delete position !"<        return;
    }
    if(k == 1)
    {
        DeleteFromHead(item);
        return;
    }
    if(k == size)
    {
        DeleteFromTail(item);
        return;
    }
    SLNode *temp = head->next;
    int count = 1;
    while(count < k-1)
    {
        count++;
        temp = temp->next;
    }
    SLNode *p = temp->next;
    temp->next = p->next;
    p->next = NULL;
    item = p->data;
    delete p;
    size--;
}
template
void SLList::ShowListMember()
{
    cout<<"List Member : ";
    SLNode *temp = head->next;
    while(temp != NULL)
    {
        cout<data<<" ";
        temp = temp->next;
    }
    cout<}

int main()
{
    int item;
    SLList list(12);

    list.Insert(0,11);
    cout<<"list number:"<    list.ShowListMember();

    list.Insert(2,14);
    cout<<"list number:"<    list.ShowListMember();

    list.Insert(2,13);
    cout<<"list number:"<    list.ShowListMember();

    list.Delete(2,item);
    cout<<"item = "<    cout<<"list number:"<    list.ShowListMember();

    list.Delete(1,item);
    cout<<"item = "<    cout<<"list number:"<    list.ShowListMember();

    list.Delete(2,item);
    cout<<"item = "<    cout<<"list number:"<    list.ShowListMember();
    return 0;
}

利用模板的好处是,SLList中的数据可以是任意的数据类型,这也就是泛型编程的概念了吧~~~~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/66175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号