C++模板

//函数模板的定义:
int abs(int n){
returnn n<0?-n:n;
}
double abs(double n){
return n<0?-n:n;
}
//**********************************************
template <class t>t abs(t n){
return n<0?-N:N;
}
//**********************************************
template <class type1,class type2>typp1 max(tpye1 a,type b){
return type1(a>b?a:b);
}
//**********************************************
class指代的是调入函数时传递的任何数据类型。
//**********************************************
//类模板的定义
class IntList{
public:
int SetItem(int index,const int &Item);
int GetItem(int index,int &Item);
private:
int Buffer[100];
};
//define a class template
template<class t,int i>class IntList{
public:
int SetItem(int index,const t &Item);
int GetItem(int index,t &Item);
private:
t Buffer[100];
}
//
template <class t,int i>int CList<t,i>::SetItem(int index,const t &Item)
{
if(index<0||index>i-1)
return 0;
Buffer[index]=Item;
return 1;
}
//
//声称类的实例
//CList(int,100)IntList;
/*
模板是特殊类型的函数或定义,可以生成整组相关函数或类,适应特定的数据类型。
函数模板定义接受各种不同类型参数的一般函数,编译器遇到函数调用时,自动生成适用于所传入的数据类型的函数版本。
定义了一个函数模板方便取代了一组重组函数的定义.
在函数模板的定义,类型蚕食 表示函数调用传递的数据项类型。

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值