首页 > 编程知识 正文

c反应蛋白59,c语言课程设计的模板

时间:2023-05-06 13:15:06 阅读:169339 作者:858

上一篇文章3359 blog.csdn.net/master _ Cui/article/details/111824152

七.类模板的模板参数为模板类

类模板的模板参数本身可以是类模板,而函数模板的模板参数不能是模板

仍然以mystack为例,新的mystack的定义如下

template typename T,int maxcount,template typename ELE,typename alloc=allocatoreletypenamecont=dequeclassmystack { public 330 } mystackT,maxcount,contoperator=(constmystackt,maxcount,CONT rval ); template typename T2,int maxcount2,template typename ELE,typename alloc=allocatoreletypenamecont2=deque mystack (template te template typename ELE,typename alloc=allocatoreletypenamecont2=dequemystackt,maxcount ~mystack (; voidpush(consttele ); void pop (; ttop(const; boolempty(const ) returnelem.empty ); }private:CONTT elem; (;

在上面的代码中,类模板的第二个模板参数也是类模板,参数为ELE,默认参与方为deque。 第二个参数也是类模板,所以不能写

如果不是template typename T,int maxcount,template typename ELE,typename alloc=allocatoreletypenamecont=dequet,编译器将报告错误

集装箱也是CONT elem; 变成了CONTT elem; 第二个参数类模板参数将依赖于第一个模板参数进行实例化

在上面的代码中,deque有两个模板参数作为模板参数。 传递到一个deque或其他容器时,会附带typename alloc,然后精确匹配模板参数。 否则,模板参数typename alloc将不正确匹配。

因此,即使alloc是默认模板参数,typename ELE和typename alloc=allocatorELE也是必不可少的。 否则,在创建模板类的实例时,将显示一条消息,指示参数不匹配

mystack的实现细节如下,与上一篇文章相差无几

template typename T,int maxcount,template typename ELE,typenamealloctypenamecontmystackt,maxcount,cont 3360: mystack () int maxcount,template typename ELE,typenamealloctypenamecontmystackt,maxcount,cont:mystack maxcount,contr val () }template typename T,int maxcount,template typename ELE,typenamealloctypenamecont mystackt,maxcount,contmystackt,maxcout if(this==rval ) {return *this; }this-elem=rval.elem; return *this; }template class T,int maxcount,template typename ELE,typenameconttemplatetypenamet 2,int maxcount2, templatetypenametypename2typename alloc2typename cont2mystackt,maxcount,cont : mystack (constmystackt 2,maxcount2) elem.clear (; while (! tmp.empty(elem.size ) maxcount (elem.push _ front ) tmp.top ) ); tmp.pop (; }}template class T,int maxcount,template typename ELE,typenameconttemplatetypenamet 2,int maxcount2, template typenamealloctypenamecont2mystackt,maxcount,CONT mystackT,maxcount,cont 33603360 operator=(constmystackt 2,mact ) }mystackT2,maxcount2,CONT2 tmp=rval; elem.clear (; while (! tmp.empty(elem.size ) maxcount (elem.push _ front ) tmp.top ) ); tmp.pop (; }return *this; }template typename T,int maxcount,template typename ELE,typenamealloctypenamecontmystackt,maxcount,cont 3360:~mystation int maxcount,template typename ELE,typenamealloctypenamecontvoidmystackt,maxcount,cont 33603360 puss ion }//cout _ _ fu fut Elem.push_back(ele; }template typename T,int maxcount,template typename ELE,typenamealloctypenamecontvoidmystackt,maxcount,cont :3: pop }catch(out_of_rangee ) {cout'out_of_range'endl; }}template typename T,int maxcount,template typename ELE,typenamealloctypenameconttmystackt,maxcount,cont :3: top ttop }catch(out_of_rangee ) {cout'out_of_range'endl; }intmain(intargc,char const *argv[] ) {mystackint,10,list si10; for(intI=0; i10; I ) si10.push(I ); }mystackdouble,20 sd20; for(intI=0; i20; I ) SD20.push ) I; (}si10=sd20; while (! si10.empty () ({coutsi10.top ) ) endl; si10.pop (; }mystackdouble,10 sd10=si10; mystackint,10 si101=si10; 返回0; }测试结果与上一篇文章相同

参考

《C++ Template》

《C++ Primer》

欢迎大家交流评论。 作者的水平有限。 如有错误,请指出

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。