模板 (函数模板语法 ,类模板与函数模板的区别,:函数模板案例,普通函数与函数模板的区别,普通函数与函数模板调用规则,模板的局限性,类模板分文件编写.cpp,Person.hpp,类模板与友元)

**01:函数模板语法:

#include<iostream>
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;

}

//函数模板
template<typename T> //声明一个模板,T是一个通用的数据类型
void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

void test01()
{
	int a = 10;
	int b = 20;

	//swapInt(a, b);
	
	//利用模板实现交换

	//1:自动类型推导

	mySwap(a, b);

	//2:显示指定类型

	mySwap<int>(a, b);




	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

}

void test02()
{
	double a = 1.0;
	double b = 2.0;


	swapDouble(a,b);
	

	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

}



int main()
{
	test01();
	test02();
	return 0;
}

02类模板与函数模板的区别

/*
总结:
1:类模板使用只能用显示指定类型方式
2:类模板中的模板参数列表可以有默认参数
*/


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

//类模板
template<class NameType, class AgeType=int>  //int模板的默认参数
class Person
{
public:

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


void test01()
{
	//Person  p1("顺悟空",324);错误,无法使用自动类型推导
	Person<string, int >  p1("顺悟空",324);  //只能用显示指定类型
	p1.showPerson();

}

//类模板在模板参数列表中可以有默认参数
void test02()
{
	Person<string>p("猪八戒",9009); //类模板中的模板参数列表 可以指定默认参数

	p.showPerson();
}
int main()
{
	test01();
	test02();
	return 0;
}

03:函数模板案例:

#include<iostream>
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和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[] = "fdgkagja";
	int len = sizeof(charArr) / sizeof(char);
	mySort(charArr, len);
	printArray(charArr, len);
}

void test02()
{
	int intArr[] = { 3,4,5,6,5,8,3,8,29 };
	int len = sizeof(intArr) / sizeof(int);
	mySort(intArr, len);
	printArray(intArr, len);
}


int main()
{
	test01();
	test02();
	return 0;
}

04普通函数与函数模板的区别

#include<iostream>
using namespace std;

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

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

//普通函数
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';     //a-97 c-99
	cout << myAdd01(a, c) << endl;

	//自动类型推导
	//cout << myAdd02(a, c) << endl;

	//显式类型转换
	cout << myAdd02<int>(a, c) << endl;

}


int main()
{
	test01();
	return 0;
}

05普通函数与函数模板调用规则

#include<iostream>
using namespace std;
/*
1;如果函数模板和普通函数都可以调用,优先调用普通函数
2:可以通过空模板参数列表 强制调用 函数模板
3:函数模板可以发生函数重载
4:如果函数模板可以产生更好的匹配,优先调用函数模板

*/


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 test01()
{
	int a = 10;
	int b = 20;

	//1;如果函数模板和普通函数都可以调用,优先调用普通函数
	myPrint(a, b);

	//通过空模板参数列表,强制调用函数模板

	myPrint<>(a, b);

	//3:函数模板可以发生函数重载
	myPrint(a, b, 100);  //3个参数


	char d = 'd';
	char f = 'd';

	myPrint(d, f);


}

int main()
{
	test01();
	return 0;
}

06模板的局限性

/*
  
  1:利用具体化的模板,可以解决自定义类型的通用化
  2:学习模板并不是为了写模板,而是在STL能运用系统提供的模板



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

//模板并不是万能的,有些特殊的数据类型,需要用具体化方式做特殊实现

*/

class Person
{
public :

	string m_Name;
	int m_Age;
	Person(string name, int age) //初始化
	{
		this->m_Name = name;
		this->m_Age = age;

	}
};






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

	}
	else
	{
		return false;
	}
}

//利用具体化Person的版本实现代码,具体化优先调用
template<>bool myCompare(Person& p1, Person& p2)
{
	if (p1.m_Name == p2.m_Name && p1.m_Age)
	{
		return true;
	}
	else
	{
		return false;
	}
}


void test01()
{
	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);

	bool ret = myCompare(p1, p2);
	if (ret)
	{
		cout << "p1=p2" << endl;

	}
	else
	{
		cout << "p1!=p2" << endl;
	}

}



int main()
{
	test01();
	test02();
	return 0;
}

07类模板分文件编写.cpp

#include<iostream>
using namespace std;
#include <string>
//#include "Person.h"
#include "Person.hpp"

/*
类模板分文件编写问题以及解决

1:直接包含 源文件
2:将.h和.cpp 中的内容写到一起,将后缀名改为.hpp

*/




void test01()
{
	Person<string, int>p("Tom", 324);
	p.showPerson();
}


int main()
{
	test01();
	return 0;
}

07-1 Person.hpp

#pragma once
#include <iostream>
using namespace std;


template <class T1, class T2>
class Person
{
public:

	T1 m_Name;
	T2 m_Age;
	Person(T1 name, T2 age);
	/*{
		this->m_Name = name;
		this->m_Age = age;
	}*/
	void showPerson();
	/*{
		cout << "姓名: " << this->m_Name << "年龄: " << this->m_Age << endl;
	}*/
};

template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	this->m_Name = name;
	this->m_Age = age;
}
template <class T1, class T2>
void Person< T1, T2>::showPerson()
{
	cout << "姓名: " << this->m_Name << "年龄: " << this->m_Age << endl;
}

08类模板与友元

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

//总结:建议全局函数做类内实现,用法简单,而且编译器容易识别


//通过全局函数 打印Person信息

//2:全局函数配合友元 类外实现  先做函数模板声明,下方再做函数模板定义 ,再做友元
template<class T1, class T2>class Person;

//如果声明了函数模板,可以将实现写在后面,否则需要将实现体写到类的前面让编译器提前看到
//template<class T1, class T2>class Person void printPerson2(Perdon<T1,T2>&p)

template<class T1, class T2>
void printPerson2(Person<T1, T2>& p)
{
	cout << "类外实现:姓名: " << p.m_Name << "年龄: " << p.m_Age << endl;

}



template<class T1 ,class T2>

class Person
{
	//全局函数
	friend void printPerson(Person<T1, T2>p)
	{
		cout << "姓名: " << p.m_Name << "年龄: " << p.m_Age << endl;
	}

	//全局函数 类外实现
	

	//template<class T1, class T2>
//	friend void printPerson2<>(Person<T1, T2>p);


public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}


private:
	T1 m_Name;
	T2 m_Age;

};

//;全局函数类外实现
//template<class T1,class T2>
//void printPerson2(Person<T1, T2>p)
//{
//	cout << "类外实现:姓名: " << p.m_Name << "年龄: " << p.m_Age << endl;
//}


//全局函数再类内实现
void test01()
{
	Person<string, int>p("Tom", 20);
	printPerson(p);
}

//全局函数再类外实现
void test02()
{
	Person<string, int>p("jon", 40);
	printPerson2(p);
}

int main()
{
	test01();
	test02();
	return 0;
}

MyArray

//创建自己通用的数组
#include<iostream>
using namespace std;

template<class T>
class MyArray
{
public:
	//有参构造函数  参数 容量
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];

	}

	//尾插法
	void Push_Back(const T & val)
	{
		//判断容量是否相等
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		this->pAddress[this->m_Size] = val;//再数组末尾插入数据
		this->m_Size++;//更新数组大小
	}

	//尾删法
	void Pop_Back()
	{
		//让用户访问不到最后一个元素,即为尾删,逻辑删除
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}

	//通过下标方式访问数组
	//arr[0]=200--要实现左值,要加上&号
	T& operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组的容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}


	//拷贝构造函数  防止浅拷贝问题

	MyArray(const MyArray& arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

//		this->pAddress = arr.pAddress;

		//深拷贝
		this->pAddress = new T[arr.m_Capacity];

		//将arr中的数据都拷贝过来
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	//operator=防止浅拷贝问题
	MyArray & operator=(const MyArray & myarray)
	{
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->m_pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		return *this;
	}



	//析构函数
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = NULL;
			this->m_Size = 0;
		}
	}
private:
	T* pAddress; //指针指向堆区开辟的真实数组
	int m_Capacity;//数组容量
	int m_Size;
	T arr[];
};

09类模板案例-数组封装

#include<iostream>
using namespace std;
#include "MyArray.hpp"
#include"string"

void printIntArray(MyArray<int>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << endl;
	}
	cout << endl;
}



//测试内置 数据类型
void test01()
{
	MyArray<int>array1(5);

	for (int i = 0; i < 5; i++)
	{
		//用尾插法向数组插入数据
		array1.Push_Back(i);
	}
	cout << "arr1输出结果为: " << endl;
	printIntArray(array1);

	cout << "array1的大小:" <<array1.getSize()<< endl;
	cout << "array的容量:" << array1.getCapacity() << endl;

	cout << "-----------------------------------------" << endl;

	MyArray<int>array2(array1);
	array2.Pop_Back();
	cout << "array2打印输出: " << endl;
	printIntArray(array2);
	cout << "array2的大小:" << array2.getSize() << endl;
	cout << "array2的容量: " << array2.getCapacity() << endl;


}


//测试自定义数据类型
class Person
{
public:
	Person() {};
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;


};

void printPersonArray(MyArray < Person>& personArr)
{
	for (int i = 0;i< personArr.getSize(); i++)
	{
		cout << "姓名:" << personArr[i].m_Name << "年龄:" << personArr[i].m_Age << endl;
	}
}



void test02()
{
	//创建数组
	MyArray<Person>pArray(10);
	Person p1("孙悟空", 23);
	Person p2("悟空", 63);
	Person p3("孙悟空", 93);
	Person p4("孙空", 23);
	Person p5("孙悟", 23);

	//插入数据

	pArray.Push_Back(p1);
	pArray.Push_Back(p2);
	pArray.Push_Back(p3);
	pArray.Push_Back(p4);
	pArray.Push_Back(p5);


	printPersonArray(pArray);

	cout << "pArray 的大小:" << pArray.getSize() << endl;
	cout << "pArray的容量:" << pArray.getCapacity() << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值