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

【c++】 模板初阶

泛型编程

写一个交换函数,在学习模板之前,为了匹配不同的参数类型,我们可以利用函数重载来实现。

代码语言:javascript

AI代码解释

void Swap(int& a, int& b) { int c = a; a = b; b = c; } void Swap(char& a, char& b) { char c = a; a = b; b = c; } void Swap(double& a, double& b) { double c = a; a = b; b = c; } //...

虽然这样似乎解决了问题,但是这样的设计写着太过麻烦,只要出现新类型就需要写新的函数,代码的复用率很低。有没有什么可以让我们一劳永逸呢?模板就可以实现这一功能。

这种通过抽象和模板化来编写可重用和灵活的代码以此提升代码的可读性和维护性,同时避免代码重复的方式称为泛型编程。

函数模板

函数模板是c++中的一类机制,通过在函数定义中使用模板参数,我们可以编写一个函数,而在调用时根据实际参数的类型自动生成相应的版本。

代码语言:javascript

AI代码解释

template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; }

这样编译器就可以根据传入的参数类型来生成对应的Swap()函数,大大提高了代码的复用率。下面我们来尝试运行一下。

代码语言:javascript

AI代码解释

template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; } int main() { int a,b; a = 1; b = 2; double c, d; c = 0.0; d = 1.2; Swap(a, b); Swap(c, d); cout << a << " " << b << endl; cout << c << " " << d << endl; return 0; }

我们发现,调用Swap()之后,int类型的ab和double类型的cd都完成了交换。但是他们是否调的是同一个函数呢?

转到反汇编:

我们发现两次调用的是不同的Swap()函数,根据传入参数类型的不同 ,编译器会生成不同的函数。然后再调用生成的函数。

函数模板的实例化

通过函数模板生成对应函数的过程叫做函数实例化。

当模板的参数只有一个时,却传入了不同类型的变量,编译器无法推导出T的类型,出现了推导错误

www.dongchedi.com/article/7594239818788389438
www.dongchedi.com/article/7594239354315817496
www.dongchedi.com/article/7594242086645187097
www.dongchedi.com/article/7594242305499382296
www.dongchedi.com/article/7594241690249167385
www.dongchedi.com/article/7594243296965411352
www.dongchedi.com/article/7594244056763695678
www.dongchedi.com/article/7594243281903649342


www.dongchedi.com/article/7594176187270693401
www.dongchedi.com/article/7594176654025589273
www.dongchedi.com/article/7594174513613636120
www.dongchedi.com/article/7594173614161723966
www.dongchedi.com/article/7594174316762726937
www.dongchedi.com/article/7594174486359114264
www.dongchedi.com/article/7594172209028399641
www.dongchedi.com/article/7594172028136112665
www.dongchedi.com/article/7594170708973158937
www.dongchedi.com/article/7594171822011318809
www.dongchedi.com/article/7594169237096940057
www.dongchedi.com/article/7594169267902857753
www.dongchedi.com/article/7594154183356727870
www.dongchedi.com/article/7594153895329448472
www.dongchedi.com/article/7594153335339647550
www.dongchedi.com/article/7594149462356804121
www.dongchedi.com/article/7594143242124427801
www.dongchedi.com/article/7594143151032959513
www.dongchedi.com/article/7594143310076477976
www.dongchedi.com/article/7594143261762585112
www.dongchedi.com/article/7594143234042380824
www.dongchedi.com/article/7594143047861420569
www.dongchedi.com/article/7594143261762257432
www.dongchedi.com/article/7594113295314305561
www.dongchedi.com/article/7594111799855956505
www.dongchedi.com/article/7594110342540673598
www.dongchedi.com/article/7594110249179578904
www.dongchedi.com/article/7594109373581492761
www.dongchedi.com/article/7594199698655724056
www.dongchedi.com/article/7594197415100793369
www.dongchedi.com/article/7594197322150904382
www.dongchedi.com/article/7594196529834181145
www.dongchedi.com/article/7594196856478368281
www.dongchedi.com/article/7594195826612781592
www.dongchedi.com/article/7594195431069762072
www.dongchedi.com/article/7594195431069270552
www.dongchedi.com/article/7594179258625802776
www.dongchedi.com/article/7594177577032991257
www.dongchedi.com/article/7594179258625409560
www.dongchedi.com/article/7594177972484309529
www.dongchedi.com/article/7594176579102884414
www.dongchedi.com/article/7594177062877839897
www.dongchedi.com/article/7594174721596473881
www.dongchedi.com/article/7594175192449008153
www.dongchedi.com/article/7594173652875444760
www.dongchedi.com/article/7594172550394069566
www.dongchedi.com/article/7594172678253265470
www.dongchedi.com/article/7594171407253275198
www.dongchedi.com/article/7594169982704173593
www.dongchedi.com/article/7594171805393650201
www.dongchedi.com/article/7594171076024812094
www.dongchedi.com/article/7594169163703271998
www.dongchedi.com/article/7594155915663180313
www.dongchedi.com/article/7594152900747100734
www.dongchedi.com/article/7594151966180426264
www.dongchedi.com/article/7594148426036904473
www.dongchedi.com/article/7594143100311192088

http://www.jsqmd.com/news/232519/

相关文章:

  • StructBERT零样本分类器优化指南:提升实时分类性能
  • Windows 10系统优化终极指南:深度解析Win10BloatRemover工具
  • 暗黑2单机终极秘籍:5招解锁隐藏玩法
  • 【c++】AVL树模拟实现
  • PvZ Toolkit植物大战僵尸修改器:轻松打造你的专属游戏体验
  • 蓝奏云直链解析工具:简化文件下载的智能解决方案
  • 数字电路实验驱动继电器控制:操作指南
  • C++ Vector 核心问题精讲
  • 专业级手机摄像头集成方案:DroidCam OBS Plugin深度解析
  • StructBERT零样本分类教程:领域适应方法
  • FlightSpy智能机票追踪系统:数据驱动的出行决策革命
  • emwin界面缩放适配方案探讨
  • QMC音频解密终极指南:三分钟解锁加密音乐文件
  • GPU内存诊断专家:MemTestCL完全使用指南与深度解析
  • PvZ Toolkit实战手册:解锁植物大战僵尸无限可能
  • Nrfr免Root工具:全球漫游网络优化的终极解决方案
  • 移动端PDF预览完整解决方案:告别兼容性困扰
  • yuzu模拟器输入精度调校指南:从基础校准到专业级优化
  • Zotero-reference插件复制功能失效的终极解决方案指南
  • 脉冲信号处理机制:数字电路实验从零实现
  • 如何高效采集抖音评论数据:零基础实战指南
  • 智能机票监控系统:让科技为你的旅行省钱
  • Linux下screen指令实战案例:后台任务管理详解
  • 零样本分类最佳实践:如何利用AI万能分类器优化工单处理
  • Umi-OCR部署实战:从下载到高效使用的完整流程
  • 终极法线贴图生成器:零基础打造专业级3D纹理效果
  • CircuitJS1 Desktop Mod:零基础也能玩转的电路模拟神器!
  • 英雄联盟皮肤修改神器:零基础打造专属游戏外观
  • 2025年必备!3分钟学会AcFun视频下载的终极方法 [特殊字符]
  • PlugY暗黑2插件:从背包焦虑到无限存储的完美蜕变