Function Templates Functions and classes help to make programs easier to write, safer, and more maintainable. However, while functions and classes do have all of those advantages, in certain cases they can also be somewhat limited by C++'s requirement that you specify types for all of your parameters. For example, you might want to write a function that calculates the sum of two numbers, similar to this:

int sum(int a, int b) {

return a+b;

}

Function Templates

We can now call the function for two integers in our main.

int sum(int a, int b) {

return a+b;

}

int main () {

int x=7, y=15;

cout << sum(x, y) << endl;

}

// Outputs 22

Function Templates

It becomes necessary to write a new function for each new type, such as doubles.

double sum(double a, double b) {

return a+b;

}

Wouldn't it be much more efficient to be able to write one version of sum() to work with parameters of any type?

Function templates give us the ability to do that!

With function templates, the basic idea is to avoid the necessity of specifying an exact type for each variable. Instead, C++ provides us with the capability of defining functions using placeholder types, called template type parameters.

To define a function template, use the keyword template, followed by the template type definition:

template

Function Templates

Template functions can save a lot of time, because they are written only once, and work with different types.

Template functions reduce code maintenance, because duplicate code is reduced significantly.

Function Templates

In our main, we can use the function for different data types:

template

int main () {

int x=72;

double y=15.34;

cout << smaller(x, y) << endl;

}

// Outputs 15

Function Templates

T is short for Type, and is a widely used name for type parameters.

It's not necessary to use T, however; you can declare your type parameters using any identifiers that work for you. The only terms you need to avoid are C++ keywords.

还有两章就要撒花完结 拿到证书了 历经一年 哈哈哈 拖的太久了 ✿✿ヽ(°▽°)ノ✿

sololearn的c++学习记录_4m11d的更多相关文章

  1. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

  2. Java 静态内部类与非静态内部类 学习记录.

    目的 为什么会有这篇文章呢,是因为我在学习各种框架的时候发现很多框架都用到了这些内部类的小技巧,虽然我平时写代码的时候基本不用,但是看别人代码的话至少要了解基本知识吧,另外到底内部类应该应用在哪些场合 ...

  3. Apache Shiro 学习记录4

    今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...

  4. UWP学习记录12-应用到应用的通信

    UWP学习记录12-应用到应用的通信 1.应用间通信 “共享”合约是用户可以在应用之间快速交换数据的一种方式. 例如,用户可能希望使用社交网络应用与其好友共享网页,或者将链接保存在笔记应用中以供日后参 ...

  5. UWP学习记录11-设计和UI

    UWP学习记录11-设计和UI 1.输入和设备 通用 Windows 平台 (UWP) 中的用户交互组合了输入和输出源(例如鼠标.键盘.笔.触摸.触摸板.语音.Cortana.控制器.手势.注视等)以 ...

  6. UWP学习记录10-设计和UI之控件和模式7

    UWP学习记录10-设计和UI之控件和模式7 1.导航控件 Hub,中心控件,利用它你可以将应用内容整理到不同但又相关的区域或类别中. 中心的各个区域可按首选顺序遍历,并且可用作更具体体验的起始点. ...

  7. UWP学习记录9-设计和UI之控件和模式6

    UWP学习记录9-设计和UI之控件和模式6 1.图形和墨迹 InkCanvas是接收和显示墨迹笔划的控件,是新增的比较复杂的控件,这里先不深入. 而形状(Shape)则是可以显示的各种保留模式图形对象 ...

  8. UWP学习记录8-设计和UI之控件和模式5

    UWP学习记录8-设计和UI之控件和模式5 1.日历.日期和时间控件 日期和时间控件提供了标准的本地化方法,可供用户在应用中查看并设置日期和时间值. 有四个日期和时间控件可供选择,选择的依据如下: 日 ...

  9. UWP学习记录7-设计和UI之控件和模式4

    UWP学习记录7-设计和UI之控件和模式4 1.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...

随机推荐

  1. less初学手记

    less语言学习手记 工具下载 在less学习中,我们都会需要随时编译我们的less文件,查看生成的css样式表是否正确,以及是否符合我们的要求.推荐一款编译软件供大家下载使用:koala,本软件支持 ...

  2. ASP.NET CORE系列【二】使用Entity Framework Core进行增删改查

    介绍 EntityFrameworkCore EF core 是一个轻量级的,可扩展的EF的跨平台版本.对于EF而言 EF core 包含许多提升和新特性,同时 EF core 是一个全新的代码库,并 ...

  3. Apache设置用户权限(2个域名。一个能访问全部文件,一个只能访问指定文件)

    可以利用apache的虚拟主机的配置设置: 2个域名一个是xxxxx.com ,一个是aaaaa.com xxxxx.com配置只访问jpg文件,aaaaa.com可以访问所有文件 <Virtu ...

  4. Windows10+Docker搭建分布式Redis集群(SSH服务镜像)(二)

    前言:上篇文章我们搭建好了Docker,下面我们开始使用Docker创建镜像,Docker命令就不介绍了.这里宿主是Windows10,cmd的管理和后期文件的复制不是很方便,将创建支持SSH的Cen ...

  5. Java:现有线程T1/T2/T3,如何确保T1执行完成之后执行T2,T3在T2执行完成之后执行。

    要实现多个线程执行完成先后,就要知道如何实现线程之间的等待,java线程等待实现是join.java的jdk中join方法实现如下: public final synchronized void jo ...

  6. delphi 10.1 Berlin 中使用自带的 MD5 校验

    uses System.Hash;//要引用这个单元哈 var Digest: TBytes; MD5: THashMD5; MD5Buf: TBytes; params: string; begin ...

  7. 利用typescript生成Swagger文档

    项目地址:https://github.com/wz2cool/swagger-ts-doc demo代码地址:https://github.com/wz2cool/swagger-ts-doc-de ...

  8. [C#]在 DotNetCore 下的 Swagger UI 自定义操作

    1.Swagger UI 是什么? Swagger UI 是一个在线的 API 文档生成与测试工具,你可以将其集成在你的 API 项目当中. 支持 API 自动同步生成文档 高度自定义,可以自己扩展功 ...

  9. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  10. [LeetCode] Relative Ranks 相对排名

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...