news 2026/3/2 12:50:40

【c++】 模板初阶

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/28 21:05:56

Coolapk-UWP桌面端深度体验:为技术爱好者打造的社区浏览新范式

Coolapk-UWP桌面端深度体验&#xff1a;为技术爱好者打造的社区浏览新范式 【免费下载链接】Coolapk-UWP 一个基于 UWP 平台的第三方酷安客户端 项目地址: https://gitcode.com/gh_mirrors/co/Coolapk-UWP 在数字内容消费日益多元化的今天&#xff0c;技术爱好者对社区平…

作者头像 李华
网站建设 2026/2/28 1:45:25

StructBERT零样本分类源码解读:模型架构设计

StructBERT零样本分类源码解读&#xff1a;模型架构设计 1. 引言&#xff1a;AI 万能分类器的诞生背景 在自然语言处理&#xff08;NLP&#xff09;的实际应用中&#xff0c;文本分类是企业智能化转型的核心能力之一。传统分类模型依赖大量标注数据进行训练&#xff0c;开发周…

作者头像 李华
网站建设 2026/2/28 19:24:29

B站视频下载技术深度解析:bilibili-downloader架构设计与实践指南

B站视频下载技术深度解析&#xff1a;bilibili-downloader架构设计与实践指南 【免费下载链接】bilibili-downloader B站视频下载&#xff0c;支持下载大会员清晰度4K&#xff0c;持续更新中 项目地址: https://gitcode.com/gh_mirrors/bil/bilibili-downloader 问题诊断…

作者头像 李华
网站建设 2026/2/25 22:02:37

PCBA焊接工艺全流程解析:超详细版指南

PCBA焊接工艺全流程实战解析&#xff1a;从入门到精通的工程指南 在电子制造的世界里&#xff0c;一块电路板能否“活”起来&#xff0c;关键就在那一道道看不见的焊点上。你有没有遇到过这样的情况&#xff1a;产品功能设计得再完美&#xff0c;却因为一个虚焊导致整机失效&am…

作者头像 李华
网站建设 2026/2/21 14:07:51

新手教程:继电器模块电路图识图基础要点

从零看懂继电器模块电路图&#xff1a;电子新手也能掌握的实战识图指南你有没有过这样的经历&#xff1f;手握一块继电器模块&#xff0c;引脚密布、灯闪闪烁&#xff0c;却不知道哪根线该接MCU&#xff0c;哪根连电源&#xff0c;更别提看懂背后的电路图了。明明只是想用Ardui…

作者头像 李华
网站建设 2026/2/27 7:51:20

YimMenu:GTA V安全使用与游戏增强全方位指南

YimMenu&#xff1a;GTA V安全使用与游戏增强全方位指南 【免费下载链接】YimMenu YimMenu, a GTA V menu protecting against a wide ranges of the public crashes and improving the overall experience. 项目地址: https://gitcode.com/GitHub_Trending/yi/YimMenu …

作者头像 李华