C++函数模板

目录

1,函数模板语法 

2,函数模板注意事项

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

4,普通函数与函数模板区别 

5,普通函数与函数模板调用规则 

6,模板的局限性 


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

例如生活中的模板:一寸照片的模板、PPT模板。

模板的特点:

模板不可以直接使用,它只是一个框架,例如你在网上下载的年度总结模板不能直接提交给老板;

模板的通用并不是万能的,例如人的身份证一寸照片的模板不能用来做宠物照片的模板。

C++有两种编程思想:一种是面向对象,一种是泛型编程;

泛型编程主要利用的技术就是模板;

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

1,函数模板语法 

函数模板的作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体指定,用一个虚拟的类型来代表。

语法:

template<typename T>

template --- 声明创建模板

typename --- 表明其后面的符号是一种数据类型,可以用class代替

T --- 通用的数据类型,名称可以替换,通常为大写字母

下边代码中一个能交换两个变量值的函数模板,可以实现交换不同数据类型变量的值。

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

template<typename T>
void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

void test()
{
	int a = 10;
	int b = 20;
	//1,自动类型推倒
	//mySwap(a, b);
	
	//2,显示指定类型
	mySwap<int>(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

}

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

函数模板利用关键字template;

使用函数模板有两种方式:自动类型推导、显示指定类型;

模板的目的是为了提高复用性,将类型参数化。 

注意:typename可以用class替代。

2,函数模板注意事项

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

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

如下代码中,int和char类型参数不能推导出一致的数据类型T。

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

template<typename T>
void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

void test()
{
	int a = 10;
	int b = 20;
	char c = 'd';
	//1,自动类型推倒
	//mySwap(a, b);
	
	//2,显示指定类型
	mySwap<int>(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

	//mySwap(a, c); //错误,推到不出一致的数据类型
	

}

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

 如下代码中,只有T的数据类型明确,函数模板才可以使用。

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

template<class T>
void func()
{
	cout << "func函数调用" << endl;
}

void test()
{

	//func();//错误,无法确定出T的数据类型

	func<int>(); //明确指定T的类型

}

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

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

利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序;

排序规则从大到小,排序算法为选择排序;

分别利用char数组和int数组进行测试;

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

template<class T>
void myPrint(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << " " ;
	}

	cout << endl;
}

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++)
		{
			if (arr[j] > arr[max])
			{
				max = j;
			}
		}

		if (i != max)
		{
			mySwap(arr[i], arr[max]);
		}
	}
}

void test()
{
	char ch[] = "dabgcef";
	int ch_len = sizeof(ch) / sizeof(char);
	mySort(ch, ch_len);

	myPrint(ch, ch_len);

	int arr[] = { 6,3,8,7,9,1 };
	int arr_len = sizeof(arr) / sizeof(int);
	mySort(arr, arr_len);
	myPrint(arr, arr_len);
}

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

该示例中写了三个函数模板,分别为排序函数模板、交换函数模板和打印函数模板。

4,普通函数与函数模板区别 

普通函数调用时可以发生自动类型转换(隐式类型转换);

函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换;

如果利用显示指定类型的方式,可以发生隐式类型转换。

如下代码中,普通函数对传入的参数进行了隐式类型转换:

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

int myAdd(int a, int b)
{
	return a + b;
}

void test()
{
	int a = 10;
	char b = 'c';

	cout << myAdd(a, b) << endl;   //隐式类型转换,将字符类型转换为int类型
}

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

如下代码中,函数模板不能发生隐式类型转换: 

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

template<class T>
T myAdd(T a, T b)
{
	return a + b;
}

void test()
{
	int a = 10;
	char b = 'c';

	cout << myAdd(a, b) << endl;   //自动类型推导时函数模板不能发生隐式类型转换
}

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

如下代码中,显示指定类型时,可以发生隐式类型转换:

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

template<class T>
T myAdd(T a, T b)
{
	return a + b;
}

void test()
{
	int a = 10;
	char b = 'c';

	//cout << myAdd(a, b) << endl;   //自动类型推导时函数模板不能发生隐式类型转换
	cout << myAdd<int>(a, b) << endl; //显示指定类型,可以发生隐式类型转换
}

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

建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T。

5,普通函数与函数模板调用规则 

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

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

函数模板也可以重载;

如果函数模板可以产生更好的匹配,优先调用函数模板。 

如下代码中,函数模板和普通函数都可以实现,优先调用普通函数:

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

//普通函数
void myPrint(int a, int b)
{
	cout << "普通函数调用" << endl;
}

//函数模板
template<class T>
void myPrint(T a, T b)
{
	cout << "函数模板调用" << endl;
}

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

如下代码中,通过空模板参数列表来强制调用函数模板:

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

//普通函数
void myPrint(int a, int b)
{
	cout << "普通函数调用" << endl;
}

//函数模板
template<class T>
void myPrint(T a, T b)
{
	cout << "函数模板调用" << endl;
}

void test()
{
	int a = 10;
	int b = 20;
	myPrint<>(a, b);
}
int main()
{
	test();
	system("pause");
	return 0;
}

如下代码中,函数模板发生了重载:

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

//普通函数
void myPrint(int a, int b)
{
	cout << "普通函数调用" << endl;
}

//函数模板
template<class T>
void myPrint(T a, T b)
{
	cout << "函数模板调用" << endl;
}

template<class T>
void myPrint(T a, T b,T c)
{
	cout << "重载函数模板调用" << endl;
}

void test()
{
	int a = 10;
	int b = 20;
	myPrint(a, b,200);
}
int main()
{
	test();
	system("pause");
	return 0;
}

如下代码中, 数模板可以产生更好的匹配,优先调用函数模板:因为此时调用函数模板相对普通函数不会经过隐式类型转换这一步骤。

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

//普通函数
void myPrint(int a, int b)
{
	cout << "普通函数调用" << endl;
}

//函数模板
template<class T>
void myPrint(T a, T b)
{
	cout << "函数模板调用" << endl;
}

template<class T>
void myPrint(T a, T b,T c)
{
	cout << "重载函数模板调用" << endl;
}

void test()
{
	char a = 'd';
	char b = 'c';

	myPrint(a, b);
}
int main()
{
	test();
	system("pause");
	return 0;
}

既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性。

6,模板的局限性 

模板的通用性不是万能的,例如T的数据类型时自定义的数据类型,就不能正常运行。

因此C++为了解决这种问题,提供模板的重载,可以对特定的类型提供具体化的模板。

如下代码中,对自定义数据类型进行对比,可以对函数模板进行重载:

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

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

	int m_Age;
	string m_Name;
};

template<class T>
bool myCompare(T a, T b)
{
	if (a == b)
	{
		return true;
	}

	else
	{
		return false;
	}
}

template<> bool myCompare(Person & p1, Person & p2)
{
	if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void test()
{
	Person p1(18, "张三");
	Person p2(18, "张三");

	bool temp = myCompare(p1, p2);
	if (temp)
	{
		cout << "p1=p2" << endl;
	}
	else
	{
		cout << "p1!=p2" << endl;
	}
}

int main()
{
	test();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值