21.C++之函数模板

学习目标:

在这里插入图片描述


学习内容:

1.模板的概念

模板就是建立通用的模具,大大提高复用性。C++提供了两种模板机制:

  • 函数模板;
  • 类模板;
1.1 函数模板基本语法

作用:建立一个通用的函数,其函数返回值类型和形参类型可以不需要具体制定,用一个虚拟的类型来代表。
语法:template<typename T>;或者template<class T>

  • template-----------声明创建模板;
  • typename---------表示其后面的符号是一种数据类型,可以用class来代替;
  • T--------------------通用的数据类型,名称可以替换,通常为大写字母;
#include<iostream>
using namespace std;
template<class T>
void MySwap(T&a, T&b)
{
	T temp = a;
	a = b;
	b = temp;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}
int main()
{
	int a = 10;
	int b = 15;
	MySwap(a, b);//自动类型推导
	//MySwap<int>(a, b);//显示指定类型
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述
函数模板注意事项:

  • 自动类型推导必须推导出一致的数据类型T,才可以使用;
  • 当两个数据类型不一致的时候,必须指明数据类型;
#include<iostream>
using namespace std;
template<class T>
void test(T a, T b)
{
	cout << "result = " << a + b << endl;
}
int main()
{
	int a = 10;
	char b = 'b';
	//test(a, b); //此时两者的数据类型不一致,不能够使用自动推导,只能使用显示类型推导
	test<int>(a, b);
	system("pause");
	return 0;
}

在这里插入图片描述

2. 函数模板案例

案例描述:
利用函数模板封装一个排序的函数,可以不同类型数组进行排序;
排序规则从大到小,排序算法为选择排序;
分别利用int数组进行测试。

#include<iostream>
using namespace std;
template<class T>
void Swap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

template<class T>
void Sort(T arr[], T len)
{
	for (int i = 0; i < len; i++)
	{
		int max = i;
		for (int j = i + 1; j < len; j++)
		{
			if (arr[max] < arr[j])
				//交换
			{
				Swap(arr[max], arr[j]);
			}
		}
	}
}
template<class T>
void Print(T arr[], T len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
int main()
{
	int arr[] = { 1,2,6,4,5,3,100,69,986 };
	int num = sizeof(arr) / sizeof(int);
	cout << "原数组为:" << endl;
	Print(arr, num);
	Sort(arr, num);
	cout << "排序后的数组为:" << endl;
	Print(arr, num);
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.普通函数和函数模板的区别

  • 普通函数调用时可以发生自动类型转换;
  • 函数模板调用时,若使用自动类型推导,不会发生数据类型的转换;
  • 在函数模板调用时,要想发生数据类型转换,必须使用显示指定类型的方式调用;
3.1 普通函数的调用
#include<iostream>
using namespace std;
void MyAdd(int a, int b)
{
	cout << "a + b = " << a + b << endl;
}
int main()
{
	int a = 10;
	char b = 'b';
	MyAdd(a, b);
	system("pause");
	return 0;
}

在这里插入图片描述

3.2 函数模板的调用

在这里插入图片描述
要想实现数据类型的转换,必须利用显示指定类型来调用!!!!!!!!!!

#include<iostream>
using namespace std;
template<class T>
void MyAdd(T a, T b)
{
	cout << "a + b = " << a + b << endl;
}
int main()
{
	int a = 10;
	char b = 'b';
	MyAdd<int>(a, b);//使用自动类型转换,程序报错,必须使用显示指定类型来转换
	system("pause");
	return 0;
}

在这里插入图片描述

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

  • 若普通函数和函数模板都可以实现对应的功能,优先调用普通函数;
  • 可以通过使用空模板参数列表来调用函数模板;
  • 函数模板也可以发生重载;
  • 若函数模板可以产生更好的匹配,优先调用函数模板;
4.1 普通函数和函数模板都可以实现对应的功能,优先调用普通函数
#include<iostream>
using namespace std;
void Print()
{
	cout << "普通函数的调用!!!!!" << endl;
}
template<class T>
void Print()
{
	cout << "函数模板的调用!!!!!!!!" << endl;
}
int main()
{
	Print();
	system("pause");
	return 0;
}

在这里插入图片描述

4.2 通过使用空模板参数列表来调用函数模板
#include<iostream>
using namespace std;
void Print(int a,int b)
{
	cout << "普通函数的调用!!!!!" << endl;
}
template<class T>
void Print(T a,T b)
{
	cout << "函数模板的调用!!!!!!!!" << endl;
}
int main()
{
	int a = 10;
	int b = 15;
	Print<>(a,b);
	system("pause");
	return 0;
}

在这里插入图片描述

4.3 函数模板重载及匹配
#include<iostream>
using namespace std;
void Print(int a,int b)
{
	cout << "普通函数的调用!!!!!" << endl;
}
template<class T>
void Print(T a,T b)
{
	cout << "函数模板的调用!!!!!!!!" << endl;
}
template<class T>
void Print(T a, T b,T c)
{
	cout << "函数模板重载的调用!!!!!!!!" << endl;
}
int main()
{
	int a = 10;
	int b = 15;
	Print<>(a,b,11);
	system("pause");
	return 0;
}

在这里插入图片描述
总结:开发中,提供函数模板就不要再使用普通函数,避免出现二义性

5. 模板的局限性

模板的通用性并不是万能的。例如:

template<class T>
void func(T a, T b)
{
	a = b;
}

当赋值代码传入的是数组,则无法实现。为了解决这种问题,提供模板的重载,可以为特定类型提供具体化模板。

#include <iostream>
#include <string>
using namespace std;
//局限性
//模板的通用性并不是万能的
 //C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

class Person
{
public:
	Person(string name, int age)
	{
		this->name = name;
		this->m_Age = age;
	}
	string name;
	int m_Age;
};

//普通函数模板
template<class T>
bool myCompare(T& a, T& b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//具体化,显式具体化的原型和定义以template<>开头,并通过名称来指出类型
//具体化优先于常规模板
template<> bool myCompare(Person& p1, Person& p2)
{
	if (p1.m_Age == p2.m_Age && p1.name == p2.name)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void test()
{
	int a = 10;
	int b = 20;
	//内置数据类型可以直接使用通用的函数模板
	bool ret = myCompare(a, b);
	if (ret)
	{
		cout << "a == b" << endl;
	}
	else
	{
		cout << "a != b" << endl;
	}
}
void test02()
{
	Person p1("Tom", 10);
	Person p2("Tom", 10);
	//自定义数据类型, 不会调用普通的函数模板
	//可以创建具体化的Person 数据类型模板,用于特殊处理这个类型
	bool ret = myCompare(p1, p2);
	if (ret)
	{
		cout << "p1 == p2" << endl;
	}
	else
	{
		cout << "p1 != p2" << endl;
	}
}

int main()
{
	
	test02();
	system("pause");
	return 0;
}

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值