c++ template实例
来源:互联网 发布:暴雪 腾讯 知乎 编辑:程序博客网 时间:2024/06/13 23:43
/* * templateTest.cpp * * Created on: 2017年11月10日 * Author: XXX */#include <iostream>using namespace std;template <typename T>T max1(const T & a, const T & b){return a>b?a:b;}template <typename T2>class Max{public:Max(T2 a1, T2 b1):a(a1),b(b1){};T2 getMaxValue(){return a>b?a:b;}private:T2 a;T2 b;};int main(void){cout<< "template fuction !!!"<<endl;int max = max1(20,25);cout<< "max = " << max <<endl;char max_c = max1('z','a');cout<< "max_c = " << max_c <<endl;float max_f = max1(5.1,3.0);cout<< "max_f = " << max_f <<endl;cout<< "template class !!!"<<endl;Max<int> int_max(20,25);int imax = int_max.getMaxValue();cout<< "get int max = " << imax <<endl;Max<char> char_max('a','z');char cmax = char_max.getMaxValue();cout<< "get char max = " << cmax <<endl;Max<float> float_max(5.1,3.0);float fmax = float_max.getMaxValue();cout<< "get float max = " << fmax <<endl;return 0;}
上面的是模板函数,下面的是模板类。
模板起到了把类型参数化,或者是类型定义做成多态性,编译器辛苦,减少程序程序员冗余代码,减少负担。
template fuction !!!
max = 25
max_c = z
max_f = 5.1
template class !!!
get int max = 25
get char max = z
get float max = 5.1
阅读全文
0 0
- Template模版实例(C++)
- C++template
- C template
- C++Template
- template 模板应用实例
- meteor 渲染template实例
- c++ template实例化
- c++ template实例
- list c++template
- Queue-C++template
- Stack-c++template
- C++Template之Priority_queue
- [C++] Template练习
- eclipse-c-template
- eclipse-c-template
- C++template ;模板学习
- list c++template
- c plus template
- 【oracle语法】-dual
- SSD笔记
- 12月4号
- JACK_C#_类和方法
- window.prompt()无效与为什么chrome console回车时要带一行undefined
- c++ template实例
- unity renderTexture 透明通道的问题
- mysql添加联合索引
- tab切换页面+cookie跳过登录页面+json解析+tree Weight绑定json数据
- 《Composing Programs》学习笔记(0)目录(关键词:软件工程)
- Python3 列表(list)
- Android源码解析之Dalvik虚拟机简要介绍和学习计划
- elasticsearch-java api之过滤
- xilinx FPGA配置加载