第十三章 模板

函数模板

函数模板使用

函数模板注意事项

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

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

普通函数和函数模板的类型转化

普通函数隐式类型转化(char转int)

函数模板正常使用不会发生隐式类型转化【自动推导】

函数模板隐式类型转化(char转int),需要指定类型

普通函数与函数模板调用

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

普通函数实现的情况

普通函数只有声明的情况

  1. 可以通过空模板参数列表来强制调用函数模板
  1. 函数模板也可以发生重载
  1. 如果函数模板可以产生更好的匹配,优先调用函数模板

模板局限性

不能比较构造的类

解决方法:1、运算符重载 2、模板重载

模板重载:

类模板

类模板使用

类模板和函数模板区别

  1. 类模板没有自动类型推导的使用方式
  1. 类模板在模板参数列表中可以有默认参数

类模板中成员函数创建时机

类模板对象做函数参数

  1. 指定传入的类型 --- 直接显示对象的数据类型
  1. 参数模板化 --- 将对象中的参数变为模板进行传递
  1. 整个类模板化 --- 将这个对象类型 模板化进行传递

类模板与继承

指定类型

子类变类模板

类模板成员函数类外实现

类模板分文件编写

person.h文件:

cpp 复制代码
#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);	//只声明
	void show();

};

person.cpp文件:

cpp 复制代码
#include "person.h"

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>::show()
{
	cout << this->m_Name << " " << this->m_Age << endl;
}

测试代码

cpp 复制代码
void test() {
	Person<string,int> p("张三",2);
	p.show();
}

解决方式1:直接包含.cpp源文件

解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

将person.h文件改为person.hpp文件【约定俗成的后缀,代表类模板】:

cpp 复制代码
#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);	//只声明
	void show();

};

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>::show()
{
	cout << this->m_Name << " " << this->m_Age << endl;
}

类模板与友元

全局函数类内实现 - 直接在类内声明友元即可

全局函数类外实现 - 需要提前让编译器知道全局函数的存在

类模板案例

属性设置

cpp 复制代码
private:
	T* pAddress;		//指针指向在堆区开辟的数组起始地址
	int m_Capacity;		//数组容量【总体容量】
	int m_Size;			//数组大小【占有的数据量】

构造函数实现

根据输入的容量进行初始化

cpp 复制代码
	MyArray(int capacity) {		//有参构造,根据容量进行初始化

		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	}

根据已有的数组进行初始化

cpp 复制代码
	//拷贝【防止浅拷贝问题】
    //构造时拷贝【当参数被声明为 const 时,函数内部不能修改这个参数的值。有助于防止意外修改传入的数据,提高代码的安全性和可维护性。】
	MyArray(const MyArray& arr) {		
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

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

		for (int i = 0; i < arr.m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];
	}

析构函数实现

cpp 复制代码
	~MyArray() {		//析构函数,如果堆区不为空,清空,防止出现野指针
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
		}
	}

重载运算符"="

cpp 复制代码
	//重载运算符"=",防止出现浅拷贝
	MyArray& operator=(const MyArray& arr) {
		//如果已有数据需要先释放
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->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 < arr.m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

		//返回对象本身
		//【this存放指向该对象的地址,*this解引用即是对象本身,MyArray&代表引用】
		//【最终效果就是返回对象本身的引用(引用可以理解为别名,使用引用可以访问该对象)】
		return *this;				
	}

实现访问堆区数据

添加数据

cpp 复制代码
	//尾插法
	void Push_Back(const T& val) {
		//判断容量是否等于大小【是否数组已经满了】
		if (this->m_Capacity == this->m_Size) {
			return;
		}

		//未满
		this->pAddress[this->m_Size] = val;
		this->m_Size++;			//更新数组大小

	}

删除数据

cpp 复制代码
	//尾删法
	void Pop_Back() {

		//如果数组为空
		if (this->m_Size == 0) {
			return;
		}

		//不为空
		this->m_Size--;			//更新数组大小

	}

访问数据

cpp 复制代码
	//通过下标访问数组
	T& operator[](int index) {		//返回引用是为了防止出现arr[i] = 10的赋值情况

		return this->pAddress[index];
	}

显示记录的参数

cpp 复制代码
	//返回数组容量
	int getCapacity() {
		return this->m_Capacity;
	}

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

MyArray.hpp总的实现

cpp 复制代码
#pragma once
#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];
	}

	//拷贝【防止浅拷贝问题】
	MyArray(const MyArray& arr) {		//构造时拷贝【当参数被声明为 const 时,函数内部不能修改这个参数的值。这有助于防止意外修改传入的数据,提高代码的安全性和可维护性。】
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;

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

		for (int i = 0; i < arr.m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];
	}

	//重载运算符"=",防止出现浅拷贝
	MyArray& operator=(const MyArray& arr) {
		//如果已有数据需要先释放
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->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 < arr.m_Size; i++)
			this->pAddress[i] = arr.pAddress[i];

		//返回对象本身
		//【this存放指向该对象的地址,*this解引用即是对象本身,MyArray&代表引用】
		//【最终效果就是返回对象本身的引用(引用可以理解为别名,使用引用可以访问该对象)】
		return *this;				
	}

	//尾插法
	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--;			//更新数组大小

	}

	//通过下标访问数组
	T& operator[](int index) {		//返回引用是为了防止出现arr[i] = 10的赋值情况

		return this->pAddress[index];
	}

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

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


	~MyArray() {		//析构函数,如果堆区不为空,清空,防止出现野指针
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
		}
	}

private:
	T* pAddress;		//指针指向在堆区开辟的数组起始地址
	int m_Capacity;		//数组容量【总体容量】
	int m_Size;			//数组大小【占有的数据量】


};

测试代码

cpp 复制代码
class Person {
public:

	string m_Name;
	int m_Age;

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

	void show()
	 {
		cout << this->m_Name << " " << this->m_Age << endl;
	}

};

void test() {
	MyArray<int> arr1(5);

	//MyArray<int> arr2(arr1);
	//MyArray<int> arr3(100);
	//arr3 = arr1;

	for (int i = 0; i < arr1.getCapacity(); i++)
		arr1.Push_Back(i);

	cout << "=====================arr1<int>=======================" << endl;
	cout << "capacity: " << arr1.getCapacity() << endl;
	cout << "size " << arr1.getSize() << endl;

	for (int i = 0; i < arr1.getSize(); i++)
		cout << arr1[i] << endl;

	arr1.Pop_Back();

	cout << "==============arr1尾删后================" << endl;
	cout << "capacity: " << arr1.getCapacity() << endl;
	cout << "size " << arr1.getSize() << endl;

	for (int i = 0; i < arr1.getSize(); i++)
		cout << arr1[i] << endl;

	MyArray<char> arr2(6);

	for (int i = 0; i < arr2.getCapacity(); i++)
		arr2.Push_Back(i+97);

	cout << "====================arr2<char>=========================" << endl;
	cout << "capacity: " << arr2.getCapacity() << endl;
	cout << "size " << arr2.getSize() << endl;

	for (int i = 0; i < arr2.getSize(); i++)
		cout << arr2[i] << endl;

	MyArray<Person> arr3(3);

	for (int i = 0; i < arr3.getCapacity(); i++) {
		Person p("man"+ std::to_string(i), i);
		arr3.Push_Back(p);
	}
	 
	cout << "====================arr3<Person>=========================" << endl;
	cout << "capacity: " << arr3.getCapacity() << endl;
	cout << "size " << arr3.getSize() << endl;

	for (int i = 0; i < arr3.getSize(); i++)
		arr3[i].show();

}