c++中模板是什么?为什么要定义模板?

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

首先:

int Max(int x, int y)
{
    return x > y ? x : y;
}

float Max(float a,float b)
{
    return a > b ? a : b;
}

通常我们想要比较不同数据类型的时候不得不定义两种不同的函数来表示区分,为了能精简代码和避免强类型的严格性和灵活性的冲突,我们就需要用到模板去改善这种情况。

二、为什么要定义模板?

强类型程序设计中,参与运算的所有对象的类型在编译时即确定下来,并且编译程序将进行严格的类型检查。为了解决强类型的严格性和灵活性的冲突。有以下3种方式解决:

1)带参数宏定义(原样替换)

2)重载函数(函数名相同,函数参数不同)

3)模板(将数据类型作为参数)

模板的使用中函数模板、类模板用的最广最繁。

函数模板

定义:

template <模板参数表>

返回类型 函数名 (参数列表)

{
  //函数体
}

注:class或typename修饰的类型参数,代表一种类型;非类型参数表达式,可以是int,long,long long类型,使用已知类型符,代表一个常量

//1.函数模版的隐式实例化
#include <iostream>
using namespace std;

template <class T>
T Max(T x, T y);    //函数模版的申明

int main()
{
    int intX = 1, intY = 2;
    double dblX = 3.9, dblY = 2.9;
    cout << Max(intX, intY) << endl;    //实参为int型,生成int型模板函数,并对第二个参数进行检查
    //或者cout << Max<int>(intX, intY) << endl;
    cout << Max(dblX, dblY) << endl;    //实参为double型,生成double型模板函数,并对第二个参数进行检查
    //或者cout << Max<double>(dblX, dblY) << endl;
    cout << Max(dblY,intX) << endl; //模板函数做不到两个参数类型不一致还可以比较

    return 0;
}

template <class T>
T Max(T x, T y)        //函数模版的实现
{
    return (x > y ? x : y);
}
//2.函数模板和函数模板的重载
#include <iostream>
using namespace std;

template < class T >
T Max(T x, T y);

template <class T>
T Max(T x, T y, T z)
{
    return x > y ? (x > z ? x : z) : (y > z ? y : z);
}

int main()
{
    int intX = 1, intY = 2, intZ = 3;
    double dblX = 3.0, dblY = 2.9;

    cout << Max<int>(intX, intY) << endl;    //调用实例化的Max(int,int)
    cout << Max<int>(intX, intY, intZ) << endl;    //调用实例化的Max(int,int,int)
    cout << Max<double>(dblX, dblY) << endl;    //显示实例化为double型,生成double型模板函数
    cout << Max('A', '8') << endl;            //隐式实例化char型,生成char型模板函数
    return 0;
}

template <class T>
T Max(T x, T y)
{
    return x > y ? x : y;
}

类模板

定义:

template<模板参数表>

class 类名
{

}

下面给出一个栈的模板实现类:

#include <iostream>
using namespace std;

#define MaxSize 10

template <class T>
class CStack
{
private:
    T data[MaxSize];
    int top;
public:
    CStack():top(-1)
    {
    }
    void Push(void);
    void Pop(void);
    bool ifEmpty()
    {
        if(top == -1)
            return true;
        else
            return false;
    }
    bool ifFull()
    {
        if(top == MaxSize-1)
            return true;
        else
            return false;
    }
    T getTop(void)
    {
        if(ifEmpty())
        {
            cout<<"栈为空,不能取栈顶!"<<endl;
            return -1;
        }
        return this->data[top];
    }
};

template <class T>
void CStack<T>::Push(void)
{
    if(ifFull())
    {
        cout<<"栈已满,不能入栈!"<<endl;
        return ;
    }
    T a;
    cin>>a;
    this->data[++top] = a;
    cout<<"元素"<<a<<"入栈!"<<endl;
}

template <class T>
void CStack<T>::Pop(void)
{
    if(ifEmpty())
    {
        cout<<"栈为空,不能出栈!"<<endl;
        return ;
    }
    T temp = this->data[top--];
    cout<<"元素"<<temp<<"出栈!"<<endl;
}


int main()
{
    CStack<int> s1;    //可以自己更换数据类型int
    int i;
    do
    {
        cout<<"\t===============================\n";
        cout<<"\t*********顺序栈类模板**********\n";
        cout<<"\t           1.入栈              \n";
        cout<<"\t           2.出栈              \n";
        cout<<"\t           3.取栈顶            \n";
        cout<<"\t           0.退出              \n";
        cout<<"\t*******************************\n";
        cout<<"\t===============================\n";
        do
        {
            cout<<"\tplease input your operator:";
            cin>>i;
            system("cls");
        }while(i!=1 && i!=2 && i!=3 && i!=0);
        switch(i)
        {
        case 1:
            s1.Push();
            system("pause");
            system("cls");
            break;
        case 2:
            s1.Pop();
            system("pause");
            system("cls");
            break;
        case 3:
            if(-1 == s1.getTop())
            {
                system("pause");
                system("cls");
                break;
            }
            else
                cout<<"栈顶元素为:"<<s1.getTop()<<endl;
            system("pause");
            system("cls");
            break;
        }
    }while(i != 0);
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值