如何维护网站,谷歌网站提交,网站做响应式还是移动端,企业网站建设合作协议范文在前面写的例子中#xff0c;模版参数一般都是 int#xff0c;或者一个类Teacher#xff0c;假设我们现在有个需求#xff1a;模版的参数要是vector#xff0c;list这种结合类型应该怎么写呢#xff1f; //当模版中的类型是 vector #xff0c;list 等集合类型的时候的处…在前面写的例子中模版参数一般都是 int或者一个类Teacher假设我们现在有个需求模版的参数要是vectorlist这种结合类型应该怎么写呢 //当模版中的类型是 vector list 等集合类型的时候的处理。//举例如下typename T是正常的模版参数MYContainer是类似vectormaplist之类的集合
templatetypename T, templateclass class MYContainer
//templatetypename T, templatetypename W class MYContainer//这一行和上一行是同样的作用只是加了一个W但是这个W不会使用只是语法上的要求
class Teacher119 {
public:T m_i;MYContainerT m_con;
public://构造方法//在参数化列表中给m_i赋值。Teacher119(T tt):m_i(tt) {//我们假设 实例化模版的的集合中是有 push_back方法的for (int i 10; i 20;i) {m_con.push_back(i);//假设真正的容器支持push_back方法,vector,list}}};//解决方案使用using 给vector起别名
templatetypename T
using MYVec vectorT, allocatorT;void main() {
//使用//按之前的写法应该这样写但是有build error,//build error原因是vector有两个参数一个是类型另一个是allocatorint//一般情况下allocatorint是有默认值的但是在这种情况下好像没有默认值//解决方案使用using 给vector起别名 //Teacher119int, vectorint myobj(10000); build errorTeacher119int, MYVec myobj(10000); //验证vectorint aa myobj.m_con;for (vectorint::iterator it aa.begin();it!aa.end();it){cout *it ;}//结果为 10 11 12 13 14 15 16 17 18 19} map情况下的处理好像不管咋写都有build error这块先剩下如果有网友知道怎么写请帮忙在留言中指导一下