template c++用法(c++模板类的用法)

本文目录
c++模板类的用法
//顺序表的实现 模板类
#include《iostream》
#include《string》
using namespace std;
const int MaxSize=100;
bool error;
template《class T》
class SeqList
{
public:
SeqList(){length=0;}; //无参构造函数
SeqList(T a,int n); //有参构造函数
~SeqList(){}; //析构
void Insert(int i,T x); //在i位置插入x
int GetLength(){return length;}; //求表长
T Del(int i); //删除第i个元素
T Get(int i); //按位查找 取第i个元素
int Locate(T x); //按值查找 求x为第几个元素
void PrintList(); //遍历并输出
private:
T data;
int length;
};
//有参构造函数
template《class T》
SeqList《T》::SeqList(T a,int n)
{
int i;
if (n》MaxSize) {cout《《"上溢"《《endl; error=true;}
else for(i=0;i《n;i++) data;
}
//在i位置插入x
template《class T》
void SeqList《T》::Insert(int i,T x)
{
int j;
if (length》=MaxSize) {cout《《"表满,无法插入"《《endl; error=true;}
else if(i》length+1||i《1) {cout《《"位置异常"《《endl; error=true;}
else
{
for(j=length-1;j》=i-1;j--)
data;
data=x;
length++;
};
}
//删除第i个元素
template《class T》
T SeqList《T》::Del(int i)
{
int x,j;
if(i》length||i《1) {cout《《"位置异常"《《endl; error=true;return 0;}
else
{
x=data;
for(j=i-1;j《=length-1;j++)
data;
length--;
return x;
}
}
//按位查找 取第i个元素
template《class T》
T SeqList《T》::Get(int i)
{
if(i》MaxSize||i《1) {cout《《"位置异常"《《endl; error=true;return -1;}
else return data;
}
//按值查找 求x为第几个元素
template《class T》
int SeqList《T》::Locate(T x)
{
int i;
for(i=0;i《length;i++)
if (data==x) return i+1; //相同的值?
return 0;
}
//遍历并输出
template《class T》
void SeqList《T》::PrintList()
{
int i;
cout《《"-----------------------------------------------------------------------";
cout《《endl;
cout《《"|";
for(i=0;i《length;i++)
cout《《" "《《data《《" |";
cout《《endl;
cout《《"-----------------------------------------------------------------------"《《endl;
}
int main()
{
cout《《"顺序表的实现(模板类)"《《endl;
cout《《"************************************"《《endl;
cout《《"* 1.插入 *"《《endl;
cout《《"* 2.按位查找 取第i个元素 *"《《endl;
cout《《"* 3.按值查找 求x为第几个元素 *"《《endl;
cout《《"* 4.删除 *"《《endl;
cout《《"* 5.输出顺序表 *"《《endl;
cout《《"* 6.输出表长 *"《《endl;
cout《《"* 7/help 输出此表 *"《《endl;
cout《《"* 8/exit.退出 *"《《endl;
cout《《"************************************"《《endl;
int flag,ins_loc,x,tab,len;
flag=0;
SeqList《int》List;
while (flag==0)
{
error=false;
cout《《"Please input the command(input 7 to get the command list):"《《endl;
cin》》tab;
switch(tab)
{
case 1:
{
cout《《"Please input the insert location:"《《endl;
cin》》ins_loc;
cout《《"Please input the insert num:"《《endl;
cin》》x;
List.Insert(ins_loc,x);
break;
}
case 2:
{
cout《《"Please input the location:"《《endl;
cin》》ins_loc;
x=List.Get(ins_loc);
if(!error)
cout 《《"The number is:"《《x《《endl;
break;
}
case 3:
{
cout《《"Please input the number you want to find in the list:"《《endl;
cin》》x;
ins_loc=List.Locate(x);
if(!error)
cout《《"The number’s location is:"《《ins_loc《《endl;
break;
}
case 4:
{
cout《《"Please input the location you want to delete:"《《endl;
cin》》ins_loc;
List.Del(ins_loc);
break;
}
case 5:
{
List.PrintList();
break;
}
case 6:
{
if(!error)
len=List.GetLength();
cout《《"The length is:"《《len《《endl;
break;
}
case 7:
{
cout《《"************************************"《《endl;
cout《《"* 1.插入 *"《《endl;
cout《《"* 2.按位查找 取第i个元素 *"《《endl;
cout《《"* 3.按值查找 求x为第几个元素 *"《《endl;
cout《《"* 4.删除 *"《《endl;
cout《《"* 5.输出顺序表 *"《《endl;
cout《《"* 6.输出表长 *"《《endl;
cout《《"* 7/help 输出此表 *"《《endl;
cout《《"* 8/exit.退出 *"《《endl;
cout《《"************************************"《《endl;
break;
}
case 8:
{
flag=1;
break;
}
default:
{
cout《《"The command is no found!"《《endl;
break;
}
}
}
return 0;
}
如图,c++里为什么template还能这样用啊为什么template尖括号里有变量(template)
模板有类型模板和非类型模板两种,你举的这种属于非类型模板。
类型模板类似于template《class T》,T是一种泛型类型,例如char、int、string或是自定义的类型A。类型模板函数template《class T》 void fun(T a),将T实例化为int那么参数a的类型就是int,实例化为A那么参数a的类型就是A。
非类型模板template《int T》同理,只不过它的泛型T不是类型而是int型常量,例如1、3、10。非类型模板的典型应用就是数组长度定义,众所周知int a}这样的写法,调用foo时必须传递一个编译期常量如10,如foo《10》(),即可在函数foo中创建一个长度为10的double数组。
template在C++有用吗
可以
template 《 typename T 》
T max( T a, T b )
{
return a 《 b ? b : a;
}
这个 max 函数就是一个模板函数,它可以传入一个 “类型”的参数,以便实现任意类型求最大值的效果。假设我们这样使用它:
int x=5, y=10;
int z=max 《int》( x, y );
这时候发生了什么呢?我们传入的“类型参数”是int,因此编译器在编译这段代码时会使用 int 来构造一个新函数:
int max( int a, int b )
{
return a 《 b ? b : a;
}
后面的事就和编译普通的函数一样了,C++编译器继续使用强类型系统编译这个函数,由强类型系统来检查这个函数是否正确。
这个过程叫做模板的“特化”,它发生在编译期,当编译器发现模板函数、模板类被使用(注意,不是定义)的时候进行的。这个系统实际上比较像宏,但是比宏更为智能。
很明显,编译器必须知道模板如何特化这个函数,因此模板函数的实现,必须在“使用点”之前,因此模板库只能通过头文件库的形式来提供。
c++ template 累加中实现累乘
我用模板和重载做了一个。不知道满足要求否?
#include 《iostream》
#include 《string》
using namespace std;
class CrazyNum
{
public:
CrazyNum(){};
CrazyNum(int n){m_n=n;};
~CrazyNum(){};
//运算法重载,执行"+"时,实际进行的是乘法
CrazyNum operator+(CrazyNum n)
{
return CrazyNum(m_n * n.m_n);
}
CrazyNum operator=(CrazyNum n)
{
m_n=n.m_n;
return CrazyNum(n.m_n);
}
//输出流的重载
friend ostream& operator《《(ostream &os, CrazyNum &n)
{
return os 《《 n.m_n;
}
private:
int m_n;
};
//把你的模板函数改成模板类了。因为main里面有个".value()"
template《typename T》
class mul
{
public:
//构造函数,要求b不得小于1
mul(T a,int b)
{
if(b《1)
cerr《《"**ERROR: b ’"《《b《《"’ is less than 1"《《endl;
m_a=a;m_b=b;
};
~mul(){};
//累加。问题中的result没有初始化的。
T value()
{
T result=m_a;
for(int i=1;i《m_b;i++)
result=result+m_a;
return(result);
};
//重载输出流。
friend ostream& operator《《(ostream &os, mul &m)
{
return os 《《 m.value();
};
private:
T m_a;
int m_b;
};
int main()
{
CrazyNum cn(2);
cout 《《 mul《float》(3.5, 1000) 《《 endl
《《 mul《string》(string("%"),5) 《《 endl
《《 mul《CrazyNum》(cn, 10).value() 《《 endl;
}

更多文章:
美国疫情为何突然消失(美国会因为疫情迅速衰落吗别天真了,这是不可能的)
2026年9月14日 21:15
嵌入式linux系统论文(嵌入式linux方向的论文写什么好驱动好写吗)
2025年7月9日 03:30
java中list方法是干嘛的(JAVA里List的详细信息)
2026年3月25日 04:00
广州疫情地图实时动态地图(疫情期间广州星海音乐厅退票详情疫情期间广州星海音乐厅退票详情图)
2025年6月11日 21:15
win10系统提供的三种用户账户级别?如何解决win10系统用户账户受保护
2025年10月11日 09:30
子选择器的表达方法(关于jQuery,$(“:button“) 中的冒号是什么意思)
2025年12月30日 12:45
read的句型(you,read,my,can,book的顺序怎么排)
2026年3月30日 18:00
canvas在线设计(canvas设计网站-用canvas画布绘制动画作为网站的banner)
2025年10月29日 12:00
自学react多久可以上手(自学前端开发需要多久才能达到就业水平)
2025年11月4日 08:45
易语言stc烧录模块(stc烧录器stc89c516rd+下载失败)
2026年7月6日 02:45
模范人物事迹ppt模板(中学生道德模范人物事迹简述材料范文(三篇))
2026年9月17日 12:15
caption怎么记忆(VBA中的“caption“应怎么翻译)
2025年9月20日 18:00
mysql一次写入10万条数据(Mysql,10W条随机批量插入(数据内容为字母和数字组合))
2026年4月4日 08:45
java 常用库(Referenced Libraries是什么意思)
2026年1月3日 12:30
accommodation的用法和短语(accommodation的动词)
2026年7月9日 22:45










