【C++学习】函数模板

模板的概念

模板就是建立通用的模具,大大提高复用性。

模板的特点:

        模板不可以直接使用,它只是一个模型

        模板的通用不是万能的

基本语法

C++中提供两种模板机制:函数模板和类模板

函数模板作用:

        建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟类型来代表 

template<typename T>

template       声明创建模板

typename     表明其后符号是一种数据类型

       T            通用的数据类型

普通代码 

#include<iostream>
#include<string>

using namespace std;

void swapint(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}
//交换两个浮点型函数
void swapdouble(double& a, double& b)
{
	double temp = a;
	a = b;
	b = temp;
}
void test01()
{
	int a = 10;
	int b = 20;
	swapint(a, b);
	cout << a << " " << b << endl;

	double c = 1.1;
	double d = 2.2;
	swapdouble(c, d);
	cout << c << " " << d << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 使用模板

#include<iostream>
#include<string>
using namespace std;

template<typename T>
//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用数据类型
void myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = t;
}
void test01()
{
	int a = 10;
	int b = 20;
	myswap(a, b);//1.自动类型推导
	cout << a << " " << b << endl;

	double c = 1.1;
	double d = 2.2;
	myswap<double>(c, d);//2.显示指定类型
	cout << c << " " << d << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

注意事项

注意:

        自动类型推导,必须推导出一致的数据类型T才可以使用

        模板必须要确定出T的数据类型,才可以使用

 

 

 函数模板案例--数组排序

#include<iostream>
#include<string>
using namespace std;

template<class T>
void myswap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}
//排序算法
template<class T>
void mysort(T arr[],int len)
{
	for (int i = 0;i < len;i++)
	{
		int max = i;
		for (int j = i + 1;j < len;j++)
		{//认定的最大值 比遍历出的数值要小
		//说明j下标的元素才是真正的最大值
			if (arr[max] < arr[j])
			{
				max = j;//更新最大值下标
			}
		}
		if (max != i)
		{
			//交换
			myswap(arr[max], arr[i]);
		}
	}
}
//打印数组
template<class T>
void printarr(T arr[], int len)
{
	for (int i = 0;i < len;i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
void test01()
{
	char chararr[] = "badcfe";
	int num = sizeof(chararr) / sizeof(char);
	mysort(chararr, num);
	printarr(chararr, num);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

普通函数与函数模板区别

普通函数调用时可以发生隐式类型转换

                        用自动类型推导,不可以发生隐式类型转换

函数模板

                        用显示指定类型,可以发生隐式类型转换

 

代码:

#include<iostream>
#include<string>
using namespace std;
int myadd(int a,int b)
{
	return a + b;
}
template<class T>
int myadd1(T a, T b)
{
	return a + b;
}
void test01()
{
	int a = 10;
	int b = 20;
	char c = 'c';
	cout << myadd(a, c) << endl;//ASCII c=99
	//不报错是因为,编译器隐式的把c类型转换成了int
	cout << myadd1<int>(a, c) << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

普通函数和函数模板的调用规则

普通函数和函数模板是可以发生函数重载的(函数名一致)

1.如果函数模板和普通函数都可以实现,优先调用普通函数

2.可以通过空模板参数列表来强制调用函数模板

3.函数模板也可以发生重载

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

 1.优先调用普通函数

#include<iostream>
#include<string>
using namespace std;
void myprint(int a, int b)
{
	cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{
	cout << "模板" << a << " " << b;
}

void test01()
{
	int a = 10;
	int b = 20;
	myprint(a,b);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 2.通过空模板参数列表来强制调用函数模板

#include<iostream>
#include<string>
using namespace std;
//通过空模板参数列表来强制调用函数模板
void myprint(int a, int b);
/*{
	cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{
	cout << "模板" << a << " " << b;
}
void test01()
{
	int a = 10;
	int b = 20;
	myprint<>(a,b);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 3.函数模板也可以发生重载

#include<iostream>
#include<string>
using namespace std;

void myprint(int a, int b);
/*{
	cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{
	cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{
	cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{
	int a = 10;
	int b = 20;
	int c = 100;
	myprint(a,b,c);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

#include<iostream>
#include<string>
using namespace std;

void myprint(int a, int b)
{
	cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{
	cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{
	cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{
	int a = 10;
	int b = 20;
	int c = 100;
	char c1 = 'a';
	char c2 = 'b';
	myprint(c1, c2);
	//普通函数可以隐式类型转换
	//但编译器认为如果走模板可以直接用,无需隐式类型转换
}
int main()
{
	test01();
	system("pause");
	return 0;
}

如果写了函数模板最好不要再写普通函数了 

 模板的局限性

#include<iostream>
#include<string>
using namespace std;
//模板并不是万能的,有些特定数据类型,需要具体化方式做特殊实现
class person
{
public:
	person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
template<class T>
bool compare(T& a, T& b)
{
	if (a == b) cout << "相等" << endl;
	else cout << "不等" << endl;
}
void test01()
{
	person a ("ll",10);
	person b ("xx",90);
	if (compare(a, b))
	{
		cout << "==" << endl;
	}
	else cout << "!=" << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 解决:

#include<iostream>
#include<string>
using namespace std;
//模板并不是万能的,有些特定数据类型,需要具体化方式做特殊实现
class person
{
public:
	person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
template<class T>
bool compare(T& a, T& b)
{
	if (a == b) cout << "相等" << endl;
	else cout << "不等" << endl;
}
//利用具体化person的版本实现
template<> bool compare(person& a, person& b)
{
	if (a.m_name == b.m_name && a.m_age == b.m_age)
	{
		return true;
	}
	else return false;
}

void test01()
{
	person a ("ll",10);
	person b ("xx",90);
	if (compare(a, b))
	{
		cout << "==" << endl;
	}
	else cout << "!=" << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

 利用具体化的模板,可以解决自定义类型的通用化

学习模板并不是为了写模板,而是在STL能运用系统提供的模板。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值