1.随意输入两个数x和y,输出最大值max。

int max(int x, int y)

{return x>y?

x:y;}

2.函数模版

(1)用一种或者多种通用类型去表示函数——函数模版。

(2)函数模版因为没有详细的数据类型。所以函数模版不可执行。

(3)作用:模板就是实现代码重用机制的一种工具,它能够实现类型參数化,即把类型定义为參数, 从而实现了真正的代码可重用性。模版能够分为两类,一个是函数模版。另外一个是类模版。

注:函数模版在调用时必须用详细的类型来替代,使之转换为一个详细函数。

3.函数模版的定义:

template <class T>

A.模版定义的关键词:template

<class T>用来说明一个通用类型T。

B.class T仅仅用来表示一种类型,也能够表示多个通用类型:

<class T1, class T2, class T3, ..., class Tn>

4.函数模版的调用

(1)将函数模版作为一个函数调用。

(2)系统将自己主动依照调用表达式实參的类型来替代函数模版中的通用类型,使之转化为一个详细函数然后运行。

example 1



#include <iostream.h>



template <class T>

T max(T x, T y)

{return x>y?

x:y;}

int main()

{

    int x = 3, y = 4;

    double d1 = 2.3, d2 = 3.4;

    long l1 = 32L, l2 = 35L;

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

    cout<<max(d1, d2)<<endl;

    cout<<max(l1, l2)<<endl;

}

程序输出:

4

3.4

35

分析:因为实參x,y为int类型,系统自己主动将模版中的类型T用int来替代。

函数转化:int max(int x, int y) {return x>y?

:x:y;}



example 2



#include <iostream.h>



template <class T>

void exchange(T &x, T &y, T &z)

{

    T t;

    if(x>y) {t = x; x = y; y = z;}

    if(x>z) {t = x; x = z; z = t;}

    if(y>z) {t = y; y = z; z = t;}

}

5.函数模版的重载

(1)函数模版同意隐性类型转换。

(2)函数模版重载时不同意重载为一个模版——函数模版的重载仅仅能够是一个详细的显性函数。

(3)函数模版的重载仅仅能够在函数模版不可调用时,系统将自己主动的通过隐式类型转换后调用函数模版时重载。

example 3



#include <iostream.h>



template <class T>

T max(T x, T y)

{

    cout<<"This is T max()."<<endl;

    return x>y?x:y;

}

double max(int x, double y)

{

    cout<<"This is double max()."<<endl;

    return x>y?

x:y;

}

int main()

{

    int x1 = 2, x2 = 3;

    double d1 = 2.3, d2 = 3.4;

    long l1 = 3L, l2 = 5L;

    cout<<max(x1, x2)<<endl; //调模版

    cout<<max(l1, l2)<<endl; //调模版

    cout<<max(x1, l2)<<endl; //调重载

    cout<<max(x1, d2)<<endl; //调重载

    cout<<max(d1, d2)<<endl; //调模版

}

程序输出:

This is T max().

3

This is T max().

5

This is double max().

5

This is double max().

3.4

This is double max().

3.4

模版的重载仅仅可以有一个详细的函数。

6.类模版

template <class T>

class Tany

{

    T x, y;

public:

    Tany(T xx, T yy):x(xx),y(yy) {}

    T getx() {return x;}

    T gety() {return y;}

};

类模板的详细化实现:

(1)通过类模板创建对象时由详细类型替代模版类型。

(2)类模版对象的定义

类名<详细类型>对象名(初始化列表)

若有多个通用类型必须表示多个详细类型

比如:Tany <int> objint(3, 4);

      Tany <double> objdouble(3.4, 5.6);

最新文章

  1. Codeforces Round #292 (Div. 2) C. Drazil and Factorial
  2. prefixfree.js_无前缀脚本
  3. CENTOS 6.4 安装oracle 10g,手工建库及升级到10.2.0.5
  4. Oracle、Mysql、Sql Server语句的区别
  5. Punycode与中文互转
  6. There is already an open DataReader associated with this Connection which must be closed first
  7. HttpSession具体解释
  8. 团队作业8——第二次项目冲刺(Beta阶段)--5.21 second day
  9. RabbitMQ在windows系统安装部署文档
  10. Oracle游标的使用示例
  11. I/O dempo
  12. Xtion pro live OpenNI2.2 Nite 2.2 安装配置1.0
  13. C# Winform将控件作为参数传递
  14. echarts给数据视图添加表格样式
  15. LeetCode--014--最长公共前缀(java)
  16. CSS3 Backgrounds相关介绍
  17. syslog、日志服务器安装、卸载详解、如何安装和卸载EventLog Analyzer
  18. 20145226夏艺华 《Java程序设计》 课堂实践
  19. 大规模实时流处理平台架构-zz
  20. 使用Python扫描网络MAC地址对应的IP地址

热门文章

  1. sql数据表的设计思路
  2. Python中的函数(3)
  3. 【练习】reserving.kr 之imageprc write up
  4. Vutrl 自己搞SS的些问题
  5. 【51nod 1154】 回文串划分
  6. C++实现顺序栈类求解中缀表达式的计算
  7. spring的IOC底层原理
  8. 69. JPA实体Bean的生命周期【从零开始学Spring Boot】
  9. .net异步编程async和await的讨论收获
  10. PTA 04-树6 Complete Binary Search Tree (30分)