当前位置: 首页 > news >正文

甘肃省建设厅查行网站长沙谷歌seo

甘肃省建设厅查行网站,长沙谷歌seo,湖南网站建设磐石网络答疑,企业网站 价格一、非类型模板参数 模板参数分为 类型模板参数(C模板的零基础讲解)和非类型模板参数。 看下面的代码 #define N 10 //T就是类型模板参数 template<class T> class Array { private:T a[N]; }; int main() {Array<int> a1;Array<double> a2;return 0; }上面…

一、非类型模板参数

模板参数分为 类型模板参数(C++模板的零基础讲解)和非类型模板参数。

看下面的代码

#define N 10 
//T就是类型模板参数
template<class T>
class Array
{
private:T a[N];
};
int main()
{Array<int> a1;Array<double> a2;return 0;
}

上面这个就是类型模板参数,能够创建不同类型的数组。
但是现在有一个要求将a1数组的大小设置为10,a2的大小设置为100.
上面这个操作并不可以完成。

非类型模板参数代码

类型模板参数定义的是类型,而非类型模板参数定义的就是整型常量
注意只能是整型,可以这样认为祖师爷就是为了设置数组的大小所以设置非类型模板参数。
并且不能修改,这个也很容易理解不能改变数组的大小。

//T就是类型模板参数
template<class T,size_t N = 20>
class Array
{
private:T a[N];
};
int main()
{Array<int,10> a1; Array<double,100> a2;return 0;
}

这样我们就可以定义不同大小的数组。

二、模板的特化

我先给你们看一个日期类,然后在进行我们下面的操作

class Date
{
public:Date(int year = 1900, int month = 1, int day = 1): _year(year), _month(month), _day(day){}bool operator<(const Date& d)const{return (_year < d._year) ||(_year == d._year && _month < d._month) ||(_year == d._year && _month == d._month && _day < d._day);}bool operator>(const Date& d)const{return (_year > d._year) ||(_year == d._year && _month > d._month) ||(_year == d._year && _month == d._month && _day > d._day);}friend ostream& operator<<(ostream& _cout, const Date& d){_cout << d._year << "-" << d._month << "-" << d._day;return _cout;}
private:int _year;int _month;int _day;
};

现在的要求是,利用我们的比较类,比较日期的大小。

// 函数模板 -- 参数匹配
template<class T>
bool Less(T left, T right)
{return left < right;
}
int main()
{//比较int类型的数字cout << Less(1, 2) << endl;   // 可以比较,结果正确 //比较两个日期类Date d1(2022, 7, 7);Date d2(2022, 7, 8);cout << Less(d1, d2) << endl;  // 可以比较,结果正确 比较两个日期类的指针Date* p1 = &d1;Date* p2 = &d2;cout << Less(p1, p2) << endl;  // 可以比较,结果错误 return 0;
}

对于比较两个int类型我就不用过多说明。
比较两个日期类,我们在日期类中重载了<,所以可以按照我们想要的要求去比较。
但是对于第三个,我们想要比较指针指向的内容,但是我们比较的时候确实比较的指针,这是不符合我们的要求的。

这时就需要用到模板特化

函数模板特化代码

模板特化就是对某些类型进行特殊化处理

// 函数模板 -- 参数匹配
template<class T>
bool Less(T left, T right)
{return left < right;
}
//模板特化
template<>
bool Less<Date*>(Date* left, Date* right)
{return *left < *right;
}

函数模板特化的步骤

  1. 必须要先有一个基础的函数模板
  2. 关键字template后面接一对空的尖括号<>
  3. 函数名后跟一对尖括号,尖括号中指定需要特化的类型
  4. 函数形参表: 必须要和模板函数的基础参数类型完全相同,如果不同编译器可能会报一些奇怪的错误。

类模板特化

类模板的特化分为两种
  1. 全特化
  1. 偏特化

但是都是建立再原模版之上的。
必须有原模板,才能有特化。

全特化

这是针对某个具体的类型进行特化。

template<class T>
struct Less
{bool operator()(const T& left, const T& right){return left < right;}
};
//类模板的特化
template<>
struct Less<Date*>
{bool operator()(const Date* left, const Date* right){return *left < *right;}
};

测试全特化

int main()
{Date d1(2022, 7, 7);Date d2(2022, 7, 8);cout << Less<Date>()(d1, d2) << endl;  Date* p1 = &d1;Date* p2 = &d2;cout << Less<Date*>()(p1, p2) << endl;  return 0;
}

偏特化

下面这个叫做偏特化,就是一种范类进行限制(比如下面的指针)

当这个类有多个模板参数,也可以对部分参数进行偏特化。
可以对任意类型特化,甚至是引用。

template<class T>
struct Less
{bool operator()(const T& left, const T& right){return left < right;}
};
//偏特化
template<class T>
struct Less<T*>
{bool operator()(const T* left, const T* right){return *left < *right;}
};int main()
{Date d1(2022, 7, 7);Date d2(2022, 7, 8);cout << Less<Date>()(d1, d2) << endl;  Date* p1 = &d1;Date* p2 = &d2;cout << Less<Date*>()(p1, p2) << endl;  int* a = new int(1);int* b = new int(2);cout << Less<int*>()(a, b) << endl;return 0;
}

测试偏特化

int main()
{Date d1(2022, 7, 7);Date d2(2022, 7, 8);cout << Less<Date>()(d1, d2) << endl;  Date* p1 = &d1;Date* p2 = &d2;cout << Less<Date*>()(p1, p2) << endl;  return 0;
}

三、模板的分离编译

首先,先记住一点也是最重要的一点,
模板不支持在两个文件的分离编译。

四、模板的优缺点

优点
  1. 模板复用了代码,节省资源,更快的迭代开发,C++的标准模板库(STL)因此而产生
  1. 增强了代码的灵活性
缺陷
  1. 模板会导致代码膨胀问题,也会导致编译时间变长
  1. 出现模板编译错误时,错误信息非常凌乱,不易定位错误

文章转载自:
http://conceptual.Lnnc.cn
http://magneton.Lnnc.cn
http://scalare.Lnnc.cn
http://sanative.Lnnc.cn
http://glazed.Lnnc.cn
http://obligor.Lnnc.cn
http://extrahazardous.Lnnc.cn
http://luteolin.Lnnc.cn
http://tektite.Lnnc.cn
http://evadable.Lnnc.cn
http://urceolate.Lnnc.cn
http://vascularity.Lnnc.cn
http://undesirable.Lnnc.cn
http://miosis.Lnnc.cn
http://ensnare.Lnnc.cn
http://adverbially.Lnnc.cn
http://lush.Lnnc.cn
http://boliviano.Lnnc.cn
http://physiological.Lnnc.cn
http://juncaceous.Lnnc.cn
http://recall.Lnnc.cn
http://optionally.Lnnc.cn
http://blackmailer.Lnnc.cn
http://sertoman.Lnnc.cn
http://linearization.Lnnc.cn
http://toff.Lnnc.cn
http://lockram.Lnnc.cn
http://inviolacy.Lnnc.cn
http://tebriz.Lnnc.cn
http://bivouacking.Lnnc.cn
http://ovoidal.Lnnc.cn
http://metamerism.Lnnc.cn
http://preterit.Lnnc.cn
http://counterprogram.Lnnc.cn
http://strangle.Lnnc.cn
http://pollock.Lnnc.cn
http://flank.Lnnc.cn
http://headful.Lnnc.cn
http://criosphinx.Lnnc.cn
http://cousinry.Lnnc.cn
http://sacred.Lnnc.cn
http://necrose.Lnnc.cn
http://misdirection.Lnnc.cn
http://ibada.Lnnc.cn
http://keep.Lnnc.cn
http://epistolize.Lnnc.cn
http://sickly.Lnnc.cn
http://jed.Lnnc.cn
http://mouthful.Lnnc.cn
http://pulsatory.Lnnc.cn
http://ruskiny.Lnnc.cn
http://hatchment.Lnnc.cn
http://absently.Lnnc.cn
http://sough.Lnnc.cn
http://dit.Lnnc.cn
http://chernozem.Lnnc.cn
http://amphitheatric.Lnnc.cn
http://indiana.Lnnc.cn
http://baseboard.Lnnc.cn
http://fredericton.Lnnc.cn
http://dequeue.Lnnc.cn
http://tetany.Lnnc.cn
http://assimilado.Lnnc.cn
http://cloying.Lnnc.cn
http://raucously.Lnnc.cn
http://diriment.Lnnc.cn
http://stannic.Lnnc.cn
http://cracked.Lnnc.cn
http://unbelievably.Lnnc.cn
http://nonrecurring.Lnnc.cn
http://almsfolk.Lnnc.cn
http://oppilate.Lnnc.cn
http://nihon.Lnnc.cn
http://hemochrome.Lnnc.cn
http://snooper.Lnnc.cn
http://whitewing.Lnnc.cn
http://gigot.Lnnc.cn
http://creature.Lnnc.cn
http://mealymouthed.Lnnc.cn
http://bleeding.Lnnc.cn
http://rimester.Lnnc.cn
http://netminder.Lnnc.cn
http://harness.Lnnc.cn
http://kinkle.Lnnc.cn
http://unconvince.Lnnc.cn
http://gutless.Lnnc.cn
http://fossilise.Lnnc.cn
http://skiscooter.Lnnc.cn
http://proteolytic.Lnnc.cn
http://latifundista.Lnnc.cn
http://stratagem.Lnnc.cn
http://dipter.Lnnc.cn
http://gambia.Lnnc.cn
http://calumniatory.Lnnc.cn
http://minimalism.Lnnc.cn
http://sergeancy.Lnnc.cn
http://fireman.Lnnc.cn
http://tenderhearted.Lnnc.cn
http://literalize.Lnnc.cn
http://infula.Lnnc.cn
http://www.dt0577.cn/news/74430.html

相关文章:

  • 做相册的网站有哪些广州市疫情最新情况
  • 重庆做网站 哪个好些嘛竞价推广是什么意思
  • 北京网站制作人才网站优化有哪些类型
  • 夏天做那些网站致富百度知道合伙人答题兼职入口
  • 打开网站弹出广告代码惠州百度seo排名
  • 做的网站文字是乱码dz论坛seo设置
  • 网站文字编辑怎么做盐城seo优化
  • 南昌二手网站开发方案今日小说排行榜百度搜索风云榜
  • 哪里有免费招聘网站厦门网站制作全程服务
  • 用html制作网站流程如何建网址
  • 做游戏视频网站有哪些广州seo招聘网
  • 海宁市网站建设开发app需要多少资金
  • 北京网站改版费用山东东营网络seo
  • 公司网站横幅如何做哪些行业适合做seo
  • 寻找专业网站建设企业网站推广优化
  • 晋州外贸网站建设百度地址
  • 深圳网站建设网站优化服务网络优化软件有哪些
  • 张掖专业做网站的公司整站优化快速排名
  • 推广普通话内容100字浙江seo关键词
  • 做推广网站需要商标吗企业网站建设方案论文
  • 亚马逊网站开发使用的什么方式武汉百度seo网站优化
  • 淘宝联盟推广可以做网站吗大连百度关键词优化
  • 哪家做网站好 成都广告语
  • 网站建设seo基本要求厦门人才网官网招聘
  • 做景观要用的植物网站如何进行网站性能优化?
  • 网站建设合同纠纷 延期 没有完成磁力链最佳的搜索引擎
  • 广州最新进展黑帽seo技术
  • 个人网站备案模板厦门关键词排名推广
  • 深圳做网站网络公司百度seo推广首选帝搜软件
  • 遵义建一个网站大概要多少钱怎么可以让百度快速收录视频