1.定义函数模板

template <typename T>
inline T const& max (T const& a, T const& b)
{// if a < b then use b else use areturn  a < b ? b : a;
}

2.使用模板函数

#include <iostream>
#include <string>
#include "max.hpp"int main()
{int i = 42;std::cout << "max(7,i):   " << ::max(7,i) << std::endl;double f1 = 3.4;double f2 = -6.7;std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;std::string s1 = "mathematics";std::string s2 = "math";std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}

输出结果

3.确定返回的参数

    ::max<double>(1,2); //ok//::max(1,1.2);//wrong::max(static_cast<double>(4),4.1);//ok

若两个参数不正确,或者不支持模板定义的特性,编译时则会出错

4.多个模板参数

template <typename T1,typename T2>
inline T1 const& max (T1 const& a, T2 const& b)
{// if a < b then use b else use areturn  a < b ? b : a;
}

示例

double i = 42.1;
std::cout << "max(7,i):   " << ::max(7,i) << std::endl;

返回值是T1,所以返回是int类型,结果是42,出错了

定义3个参数,第3个参数用于表示返回值类型

template <typename T1,typename T2,typename T3>
inline T3 const& max (T1 const& a, T2 const& b)
{// if a < b then use b else use areturn  a < b ? b : a;
}

测试

double i = 42.1;
std::cout << "max(7,i):   " << ::max<int,double,double>(7,i) << std::endl;

返回正确的42.1

5.模板函数重载

// maximum of two int values
inline int const& max (int const& a, int const& b)
{return  a < b ? b : a;
}// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{return  a < b ? b : a;
}// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{return ::max (::max(a,b), c);
}int main()
{::max(7, 42, 68);     // calls the template for three arguments::max(7.0, 42.0);     // calls max<double> (by argument deduction)::max('a', 'b');      // calls max<char> (by argument deduction)::max(7, 42);         // calls the nontemplate for two ints::max<>(7, 42);       // calls max<int> (by argument deduction)::max<double>(7, 42); // calls max<double> (no argument deduction)::max('a', 42.7);     // calls the nontemplate for two ints
}

特别注意::max<>(7, 42);这句的写法 
注意:自动类型转换

::max('a', 42.7); 只适用于常规函数,'a’会完成自动类型转换

6.字符串重载的例子

#include <iostream>
#include <cstring>
#include <string>// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{return  a < b  ?  b : a;
}// maximum of two pointers
template <typename T>
inline T* const& max (T* const& a, T* const& b)
{return  *a < *b  ?  b : a;
}// maximum of two C-strings
inline char const* const& max (char const* const& a,char const* const& b)
{ return  std::strcmp(a,b) < 0  ?  b : a;
}int main ()
{int a=7;int b=42;::max(a,b);      // max() for two values of type intstd::string s="hey";std::string t="you";::max(s,t);      // max() for two values of type std::stringint* p1 = &b;int* p2 = &a;::max(p1,p2);    // max() for two pointerschar const* s1 = "David";char const* s2 = "Nico";::max(s1,s2);    // max() for two C-strings
}

注意每个重载函数必须存在着一定的差异.

7.总是把重载函数定义在调用之前

// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{return  a < b ? b : a;
}// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{return max (max(a,b), c);  // uses the template version even for ints
}                              // because the following declaration comes// too late:
// maximum of two int values
inline int const& max (int const& a, int const& b)
{return  a < b ? b : a;
}

3参数的max调用的是2参数模板函数,而非最后定义的max非模板函数

8.std:string和char

#include <string>// note: reference parameters
template <typename T>
inline T const& max (T const& a, T const& b)
{return  a < b  ?  b : a;
}int main()
{std::string s;::max("apple","peach");   // OK: same type::max("apple","tomato");  // ERROR: different types::max("apple",s);         // ERROR: different types
}

“apple”会被转成 char[6]数组,所以比较要求数组维度长度相同,第二条”tomato”与”apple”长度不符合,即出错.

s是std:: string类型,而非char数组,所以也出错

9.以非引用参数方式传递

#include <string>// note: nonreference parameters
template <typename T>
inline T max (T a, T b)
{return  a < b  ?  b : a;
}int main()
{std::string s;::max("apple","peach");   // OK: same type::max("apple","tomato");  // OK: decays to same type::max("apple",s);         // ERROR: different types
}

只有最后类型不符合的编译不通过

c++ template笔记(1)模板函数相关推荐

  1. c++ template笔记(2)模板类

    1.自定义Stack模板类 #include <vector> #include <stdexcept>template <typename T> class St ...

  2. C++ STL学习笔记 : 1. template 模板函数

    本篇文章是学习C++ STL库的第一篇笔记,主要记录了使用template关键字创建模板函数的方法. 下面用一个非常简单的例子解释模板函数的用法 : #include <iostream> ...

  3. [转]C++函数模板与模板函数

    1.函数模板的声明和模板函数的生成 1.1函数模板的声明 函数模板可以用来创建一个通用的函数,以支持多种不同的形参,避免重载函数的函数体重复设计.它的最大特点是把函数使用的数据类型作为参数. 函数模板 ...

  4. [c++][语言语法]函数模板和模板函数 及参数类型的运行时判断

    参考:http://blog.csdn.net/beyondhaven/article/details/4204345 参考:http://blog.csdn.net/joeblackzqq/arti ...

  5. 笔记②:牛客校招冲刺集训营---C++工程师(面向对象(友元、运算符重载、继承、多态) -- 内存管理 -- 名称空间、模板(类模板/函数模板) -- STL)

    0618 C++工程师 第5章 高频考点与真题精讲 5.1 指针 & 5.2 函数 5.3 面向对象(和5.4.5.5共三次直播课) 5.3.1 - 5.3.11 5.3.12-14 友元 友 ...

  6. C++ and Java template class and function 模板类和模板函数

    在C++和Java的泛式编程中,模板template的使用是必不可少的,但是Java中没有template关键字,所以两者的写法还是有些许区别的,请参见如下代码: Java的模板 // Java pu ...

  7. C++笔记7:C++提高编程1:模板—[函数模板和类模板]

    0820 C++提高编程: 1.模板-[函数模板和类模板] 2.初识STL 3.STL-常用容器 4.STL-函数对象 5.STL-常用算法 C++提高编程引言: C++除了面向对象编程思想,还有泛型 ...

  8. 简述类模板函数模板template (typename T)

    相信很多刚入门c++的同学在学数据结构或者查看大佬的代码中,我们都会看到: template <typename T> 类模板; template<typename T> cl ...

  9. 传智播客PHP笔记05-thinkphp框架-视图渲染、display,fetch,模板替换,模板变量的赋值与实现,系统变量,模板函数,模板运算符,foreach,if,比较标签,volist标签

    1.视图概述 将具体的视图模板进行输出显示,有两个方法 display:获取具体要输出的内容,然后直接输出 fetch:获取具体要输出的内容,但不会自动输出 2.display的使用(输出模板内容) ...

最新文章

  1. 2021年大数据常用语言Scala(十三):基础语法学习 函数 重点掌握
  2. 提高 GPU 训练利用率的Tricks
  3. Cell子刊:建立因果关系-合成菌群在植物菌群研究中的机会
  4. metaSPAdes:新型多功能宏基因组拼接工具
  5. 理解C#值类型与引用类型(收藏)
  6. 堆密度测定的意义_堆密度的测量
  7. blue pill Flash 128KB的传言
  8. twitter自定义api_为Twitter4j创建自定义SpringBoot Starter
  9. 亚麻纤维截面形态_天然丝纤维蚕丝
  10. mybatisplus坑 insert标签insert into select无参数问题
  11. linux 查看磁盘分区,文件系统,使用情况的命令和相关工具介绍,Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍...
  12. Linux find xargs rm .orig
  13. Ubuntu安装Atom编辑器
  14. Atitit.会员卡(包括银行卡)api的设计
  15. liblinear参数及使用方法(原创)
  16. 使用树莓派打造家庭监控系统
  17. FlyAI小课堂:Fbank和MFCC介绍-理论和代码
  18. 去除安卓apk中的广告
  19. 深度学习平台配置 Pytorch+RTX3090+Pycharm
  20. 聆听C++语言创建者的教诲

热门文章

  1. python 绘制平滑曲线_用python绘制概率图形曲线
  2. android分钟倒计时,Android 三十分钟倒计时
  3. 俄罗斯“指尖旋风”席卷南京
  4. Web Api 基于Zookeeper的服务注册与发现
  5. iPhone X系列 的获取 - 安全区顶部和底部高度
  6. python 内置递归
  7. Radware发布2015-2016年全球应用及网络安全报告
  8. Android Studio 2.1.2 升级到 2.2之后,gradle 编译版本更新为2.2.0,databinding报错
  9. LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)...
  10. 灰色系统与灰色预测模型