c++模版

c++ 函数模版

c++ 另一种编程思想是泛型编程,利用的技术是模版。两种模版机制是函数模版和类模版。
函数模版语法:template或者template
template是模版关键字
typename是函数模版
class 是类模版

#include<iostream>
using namespace std;

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

void test01() {
	int a = 10;
	int b = 20;
	//自动类型推导
	mySwap(a, b);
	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
}

void test02() {
	int a = 90;
	int b = 80;
	//显示指定类型
	mySwap<int>(a,b);
	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
}
int main() {
	test01();
	test02();
}

c++函数模版的注意事项

1.自动类型推导,必须推导出一致的数据类型T,才可以使用
2.模版必须要确定出T的数据类型,才可以使用

#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 << "func的调用" << endl;
}

void test01() {
	int a = 10;
	int b = 20;
	char c = 'c';
	mySwap(a, b);
	//mySwap(a, c);//会报错,因为自动类型推导,必须推导出一致的数据类型才可以使用;
	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
}

void test02() {
	//func(); //会报错,因为不知道T的数据类型
	func<int>();
}
int main()
{
	test01();
	test02();
}

c++ 实现一个通用的对数组进行排序的函数

[规则:从大到小]
[算法:选择排序]
[测试:char数组,int数组]

#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 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[max] < arr[j])
			{
				max = j;
			}
		}
		if (max != i)
		{
			mySwap(arr[max], arr[i]);
		}
	}	
}
//打印算法
template<class T>
void printArray(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
void test01() {	
	//测试char数组
	char charArr[] = "badcfe";
	int num = sizeof(charArr) / sizeof(char);
	mySort(charArr, num);
	printArray(charArr, num);
}
void test02() {
	//测试整数数组
	int intArr[] = { 7,5,1,3,9,2,4,6,8 };
	int num = sizeof(intArr) / sizeof(int);
	mySort(intArr, num);
	printArray(intArr, num);
}
int main()
{
	test01();
	test02();
}

c++ 普通模版与函数模版的区别

[1]普通函数调用可以发生隐式类型转换
[2]函数模版用自动类型推导,不可以发生隐式类型转换
[3]函数模版用显示指定类型,可以发生隐式类型转换

#include<iostream>
using namespace std;
#普通函数
int myAdd01(int a, int b) 
{
	return a + b;
}
#函数模版
template<class T>
T myAdd02(T a, T b)
{
	return a + b;
}
#测试普通函数
void test01() {
	int a = 10;
	int b = 20;
	char c = 'c';
	myAdd01(a, b);
	myAdd01(a, c);
	cout << "a+b= " << myAdd01(a, b) << endl;
	cout << "a+c= " << myAdd01(a, c) << endl; //普通函数调用可以发生隐式类型转换。
}
#测试函数模版
void test02() {
	int a = 30;
	int b = 50;
	char c = 'c';
	myAdd02(a,b);
	cout << "a+b= " << myAdd02(a,b) << endl;
	cout << "a+c= " << myAdd02(a, c) << endl; //函数模版用自动类型转换会报错,不可以发生隐式类型转换。
	cout << "a+c= " << myAdd02<int>(a, c) << endl; //函数模版用显示指定类型时,正确运行。可以发生隐式类型转换。
}
int main() {
	test01();
	test02();
}

c++ 普通函数与函数模版的调用规则

1.如果函数模版与普通函数都可以调用,优先调用普通函数
2.可以通过空模版参数列表调用函数模版
3.函数模版可以发生函数重载
4.如果函数模版可以产生更好的匹配,优先调用函数模版

#include<iostream>
using namespace std;

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

template<class T>
void myPrint(T a,T b) {
	cout << "函数模版的调用" << endl;
	
}
void test01() {
	int a = 10;
	int b = 30;
	myPrint(a, b);   //普通函数和函数模版都可以调用的时候,优先调用普通函数;
	myPrint<>(a, b); //采用空模版参数列表可以实现函数模版的调用;
	char c1 = 'c';
	char c2 = 'd';
	myPrint(c1, c2);//如果函数模版可以产生更好的匹配,优先调用函数模版;
}
int main() {
	test01();
}

模版的局限性

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板
    *提供模版的重载,为这些特定的类型提供具体化的模版
#include<iostream>
using namespace std;
#include<string>

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

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.mAge == p2.mAge && p1.mName == p2.mName)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void test01() {
	Person p1("Tom", 10);
	Person p2("Tom", 10);
	myCompare(p1, p2);
	bool res = myCompare(p1, p2);
	if (res)
	{
		cout << "a = b" << endl;
	}
	else
	{
		cout << "a != b" << endl;
	}
}

int main() {

	test01();
}

类模版语法

template
后面跟类的定义就是类模版,后面跟函数的定义就是函数模版;

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

template<class NameType,class AgeType>
class Person {
public:
	Person(NameType name, AgeType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void showPerson() {
		cout << "name: " << this->m_Name <<"\t" << "age: " << this->m_Age << endl;
	}
public:
	NameType m_Name;
	AgeType m_Age;
};

void test01() {
	Person<string, int>p1("孙悟空", 999);
	p1.showPerson();
}
int main() {
	test01();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值