我的小小学习笔记(7)之C++------模板之模板函数

模板

模板是泛型编程的基础。所谓泛型编程就是编写与类型无关的逻辑代码,是一种复用的方式(但是跟复用不一样)(继承也是为了实现复用)。模板分为模板函数和模板类。

模板函数:

假设现在要实现一个比较两个数是否相等的重载函数。

比较int,支持=,但是string不支持(以前一个一个字符比,现在支持重载)

函数模板格式

template<class形参名1,class形参名2,...,class形参名n>

返回类型 函数名(参数列表)

{...}

模板形参定义可以用class,也可以用typename,含义相同

template<typenameT>

下面给出些例子感受一下。。。

(1)这个地方调用3个函数

#include <string>
using namespace std;
template<class T>
bool ISEqual(T l, T r)
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 2) << endl;//int
	cout << ISEqual(string("11"), string("11")) << endl;//string
	cout << ISEqual('1', 'a') << endl;//char
	system("pause");
	return 0;
}


(2)

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(T l, T r)
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 2) << endl;//int
	cout << ISEqual(string("11"), string("12")) << endl;//string
	cout << ISEqual('1', 'a') << endl;//char
	system("pause");
	return 0;
}



没有模板函数实例化,不会生成对应的代码

基本格式还是会检查的,但是函数内部出问题不会检查出来。

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l,const T& r)//形参,用T时,尽量用const,不用,像string深拷贝传参,代价很大
{
	return l == r;
}
int main()
{
	ISEqual(1, 2);//实参
	//ISEqual(string("11"), string("12"));
	//ISEqual('1', 'a');//char
	system("pause");
	return 0;
}


看下一个程序

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l,const T& r)//形参,用T时,尽量用const,不用,像string深拷贝传参,代价很大
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 1) << endl;
	cout << ISEqual(1, 1.2) << endl;
	system("pause");
	return 0;
}


原因:bool ISEqual(const T&l,const T&r)//要把T推演成int,一个T既是int又是double,就出问题了

改正方法一:

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l,const T& r)
{
	return l == r;
}
template<class T1, class T2>
bool ISEqual(const T1& l, const T2& r)
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 1) << endl;
	cout << ISEqual(1, 1.2)<< endl;
	system("pause");
	return 0;
}


cout << ISEqual(1, 1.2)<< endl;为假,T1推演成int,T2推演成double,int和double比较时会发生隐式类型的转化,l会转,当两个类型为相近类型时,范围小的会往范围大的转(char转向int,int转向unsigned int,有符号向无符号转)。

改正方法二(1):


#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l,const T& r)
{
	return l == r;
}
template<class T1, class T2>
bool ISEqual(const T1& l, const T2& r)
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 1) << endl;
	//cout << ISEqual(1, 1.2)<< endl;
	//显示实例化,指定模板参数,就不会推演
	cout << ISEqual<int,int>(1, 1.2) << endl;
	system("pause");
	return 0;
}

改正方法二(2):


#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l,const T& r)
{
	return l == r;
}
//template<class T1, class T2>
//bool ISEqual(const T1& l, const T2& r)
//{
//	return l == r;
//}
int main()
{
	cout << ISEqual(1, 1) << endl;
	//cout << ISEqual(1, 1.2)<< endl;
	//显示实例化,指定模板参数,就不会推演
	cout << ISEqual<int>(1, 1.2) << endl;
	system("pause");
	return 0;
}


(3)


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l, const T& r)
{
	return l == r;
}
//template<class T1, class T2>
//bool ISEqual(const T1& l, const T2& r)
//{
//	return l == r;
//}
int main()
{
	cout << ISEqual(1, 1) << endl;
	//cout << ISEqual(1, 1.2)<< endl;
	//显示实例化,指定模板参数,就不会推演
	cout << ISEqual<int>(1, 1.2) << endl;
	cout << ISEqual<double>(1, 1.2) << endl;
	system("pause");
	return 0;
}



调用谁呢?


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
template<class T>
bool ISEqual(const T& l, const T& r)
{
	return l == r;
}
bool ISEqual(int l,int r)
{
	return l == r;
}
int main()
{
	cout << ISEqual(1, 2) << endl;
	ISEqual(string("11"), string("11"));
	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值