美文网首页
C++ 函数模板template

C++ 函数模板template

作者: 魂之挽歌w | 来源:发表于2017-12-22 19:30 被阅读20次

C++可以实现函数的重载,而有一些函数只是参数不同,函数体完全一样,这样的函数可以用一个函数模板来简化:

模板的一般形式为

template <typename T>

或者

template<class T>

例:

#include<iostream>

using namespacing std;

template <typename T>

T max(T a,T b,T  c){//可以看到相当于java中的 泛型

if(a>b)  a=b;

if(c>a)   a=c;

return a;

}

相关文章

网友评论

      本文标题:C++ 函数模板template

      本文链接:https://www.haomeiwen.com/subject/pppxgxtx.html