您当前的位置: 首页 >  c++

txwtech

暂无认证

  • 3浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

cc36a_demo-txwtech-c++_CppPrimer_函数模板

txwtech 发布时间:2020-01-20 19:10:27 ,浏览量:3

//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)             
关注
打赏
1665060526
查看更多评论
0.0380s