c++中的函数模版

c++中的一种编程思想称为 泛型编程 ,主要利用的技术就是模版

c++提供了两种模版机制:函数模版和类模版

1、函数模板

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

template<typename T>
//函数声明或者定义
//template---声明创建模版
//typename---表明其后面的符号是一种数据类型,可以用class代替
//T---通用的数据类型,名称可以替换,通常为大写字母
//基础模板语法
#include <iostream>
#include <cstring>

using namespace std;

//function template
template<typename T>
//declare a template ,tell the compiler don't report error regarding the following code T,the "T" is an universal data type 
void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

//两个整形交换函数
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;
	//two usage of template:
	//1.automatically infer
    mySwap(a, b);
	//2.Specify the type explicitly
	mySwap<int>(a, b);
	//swapInt(a, b);
	cout << a << " " << b << endl;
}
int main()
{
	test01();


	system("pause");
	return 0;
}


//基础函数模板的注意事项
#include <iostream>
using namespace std;

 template<class T>
 void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}
 template<class T>
void func( )
{
	
	cout << "diaoyong" << endl;
}
void test02()
{
	func<int>();
}
//此时没有办法调用,必须显式指定T的数据类型
void test01()
{
	int a = 10;
	char c = 'c';
	//函数模板注意事项:1、自动类型推导,必须导出一致的数据类型T,才可以使用
	//mySwap(a, c);//error ,数据类型不一致
	//2、模板必须要确定出T的数据类型才可以使用   	
}
int main()
{
	test02();
	system("pause");
	return 0;
}
//3.函数模板案例
#include <iostream>
#include<string>
using namespace std;
//利用函数模板封装一个排序的函数,对不同数据类型的数据进行排序
//排序规则从大到小,排序算法为选择排序
//分别利用char数组和int数组进行测试

//交换函数模板
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)//交换max和j下标的元素
		{
			mySwap(arr[max], arr[i]);
		}
	}
}


void test01()
{
	//测试char数组
	char charArr[] = "badcfe";
	mySort(charArr, sizeof(charArr) / sizeof(char));
	cout << charArr << endl;
}
void test02()
{
	int intArr[] = { 7,3,2,4,5 };
	int num = sizeof(intArr) / sizeof(int);
	mySort(intArr, num);
}
int main()
{
	test01();
	
	system("pause");
	return 0;
}
//4.普通函数和模板函数的区别
#include <iostream>
#include <cstring>

using namespace std;
//普通函数与函数模版之间的区别
//普通函数调用时可以发生自动类型转换(隐式类型转换)
//函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
//如果利用显示指定类型的方式,可以发生隐式类型转换
int myAdd01(int a, int b)
{
	return (a + b);
}
void test01()
{
	int a = 10;
	int b = 20;
	char c = 'c';
	cout << myAdd01(a, b) << endl;
	//自动类型推导
	cout << myAdd02(a, b)<<endl;
	//显式类型推导
	cout << myAdd02<int>(a, c) << endl;
}
template<class T>
T myAdd02(T a,T b)
{
	return a + b;
}
int main()
{
	test01();


	system("pause");
	return 0;
}

5.普通函数和模版函数的调用规则
#include <iostream>
#include <cstring>

using namespace std;
//如果函数模板和普通函数都可以实现,优先调用普通函数
//可以通过空模板参数列表来强制调用函数模板
//函数模板也可以发生重载
//如果函数模板可以产生更好的匹配,优先调用函数模板


void myPrint(int a,int b) {
	cout << "putong" << 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 test01()
{
	int a = 10;
	int b = 20;
	myPrint(a, b);//此时如果只声明了普通函数,但是普通函数没有实现,那么此时会报错,仍然优先调用普通函数
	myPrint<>(a, b);//通过空模板参数列表,强制使用函数模板
	myPrint(a, b, 100);
	char c1 = 'a';
	char c2 = 'b';
	myPrint(c1, c2);//编译器认为如果用普通的,还要把char转换成int,倒不如把char直接转换成T,然后直接调用,就是只要推出T就行了

	//一般在实际开发中不会出现普通函数和模板函数同时出现的情况,但是如果真的出现了这种情况,要知道他的底层调用规则
}
int main()
{
	test01();


	system("pause");
	return 0;
}
//6.没有办法比较的时候如何处理?函数模板存在局限性
bool myCompare(T & a,T & b)
{
    if(a==b)
    else...
}

如果是传入Person?
1.函数重载  class MyClass {
public:
    int value;

    // 重载==运算符的成员函数版本
    bool operator==(const MyClass& other) const {
        return this->value == other.value;
    }
};

// 重载==运算符的友元函数版本
bool operator==(const MyClass& obj1, const MyClass& obj2) {
    return obj1.value == obj2.value;
}
2.//利用具体化Person的版本来实现代码,具体化优先调用
template <> bool myCompare(Person &a , Person & b)
{
if(p1.name==p2.name&&p1.age==p2.age)
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值