//cc36a_demo-txwtech-c++_CppPrimer_函数模板 //函数模板-》实例化-》函数 //模板形参,T就是形参 //template //typename与class写法都可以-建议用这个,避免与类的class混淆 //template ,这个与c++类的class没有关系 //调用什么类型-函数模板自动匹配函数的类型,自动变成相应类型
//3.顺序队列 //内部是数组--类模板 //4.链式队列 //内部是链表--类模板
//cc36a_demo-txwtech-c++_CppPrimer_函数模板
//函数模板-》实例化-》函数
//模板形参,T就是形参
//template //typename与class写法都可以-建议用这个,避免与类的class混淆
//template ,这个与c++类的class没有关系
//调用什么类型-函数模板自动匹配函数的类型,自动变成相应类型
//3.顺序队列 //内部是数组--类模板
//4.链式队列 //内部是链表--类模板
#include
#include
#include //文件流
#include //字符串流
using namespace std;
//int compare(const double &v1,const double &v2)
//{
// if (v1 < v2) return -1;
// if (v1 > v2) return 1;
// return 0;
//}
//int compare(const string &v1, const string &v2)
//{
// if (v1 < v2) return -1;
// if (v1 > v2) return 1;
// return 0;
//}
//用函数模板来写
//函数模板例子1
template //TT代表任意一种类型
int compare(const TT &v1, const TT &v2) //函数模板自动匹配函数的类型
{
if (v1 < v2) return -1;
if (v1 > v2) return 1;
return 0;
}
//函数模板例子2
template
T absVal(T val)
{
return val > 0 ? val : -val;
}
//函数模板例子3
template
T1& print(T1& s,T2 val)
{
s v2 ? v1 : v2;
}
int main()
{
double dval = -0.88;
float fval = -12.33;
string oristr = "this is a test";
string desstr;
ostringstream oss(desstr);//输出字符串流
ofstream outFile("result.dat");//输出文件流
print(cout, -3)
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?