C++_模板_学习笔记

模板的作用:简历通用的模具,提高复用性。
C++提供两种模板机制:函数模板类模板

1 函数模板

1.1 函数模板语法

template<typename T>
函数声明或定义

template — 声明创建模板
typename — 表明其后面的符号是一种数据类型,可以用class代替
T — 通用的数据类型,名称可以替换

例如:要实现两个数交换,每种情况都写一遍太麻烦了,使用模板来搞:
template<typename T>
void mySwap(T& a, T& b)
{
	T temp = a;
	a = b;
	b = temp;
}

int main()
{
	int a1 = 10;
	int b1 = 20;
	float a2 = 10;
	float b2 = 20;
	// 调用方法1、自动类型推导
	mySwap(a1, b1);
	// 调用方法2、显式指定类型
	mySwap<float>(a2, b2);
	return 0;
}

注意事项1. 模板的使用可以通过自动类型推导,但必须推导出一致的数据类型T才可以使用。

	int a1 = 10;
	float b1 = 20;
	mySwap(a1, b1);

这样做是非法的,推导出来的T不一致。
注意事项2. 模板必须要确定出T的数据类型才可以使用

template <typename T>
void func()
{
	cout << "OMG" << endl;
}
int main()
{
	func(); //这样做是非法的,因为没有指出确定的T。
	func<int>(); // 对于这种情况,要指定数据类型才能正常运行。
}

1.2 函数模板和普通函数的区别

  1. 普通函数调用可以发生隐式类型转换
int func(int a, int b){...}
int main()
{
	int a = 10;
	char b = 'd';
	func(a , b);
}
该函数会将b强制转换为int,再执行函数
  1. 函数模板用自动类型推导时,不可以发生隐式类型转换
template<typename T>
T func(T a, T b){...}
int main()
{
	int a = 10;
	char b = 'd';
	func(a , b);
}
这样会报错
  1. 函数模板用显式指定类型时,可以发生隐式类型转换
template<typename T>
T func(T a, T b){...}
int main()
{
	int a = 10;
	char b = 'd';
	func<int>(a , b);
}
这样会将b强制转换为int,再运行函数。

建议使用显式指定类型来调用模板

2 类模板

2.1 类模板基本语法

template<typename T>
类
template<class NameType, class AgeType>
//这里的class和typename一样的,有的人为了方便区分,写类模板时用class
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void showPerson()
	{
		cout << m_name << m_age << endl;
	}
	NameType m_name;
	AgeType m_age;
};

int main()
{
	Person<string, int> p1 ("卢本伟", 25) ;
	p1.showPerson();
	return 0;
}

类模板没有自动类型推导,必须显式指定类型。
类模板中的成员函数只有当程序运行时才会创建;而普通类中的成员函数在编译时就会创建。

2.2 类模板对象做函数参数

目的:类模板实例化出来的对象,作为函数的参数。
有三种传入方式:

  1. 指定传入类型:直接显式对象的数据类型
  2. 参数模板化:将对象中的参数变为模板进行传递
  3. 类模板化:将这个对象类型模板化进行传递
    最常用的是指定传入类型
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	void ShowPerson()
	{
		cout << "姓名:" << this->m_name << "\t年龄:" << this->m_age << endl;
	}
	T1 m_name;
	T2 m_age;

};
//1、指定传入类型
void PrintPerson1(Person<string,int> &p)
{
	p.ShowPerson();
}
//2、参数模板化
template<typename T1, typename T2>
void PrintPerson2(Person< T1, T2 >& p)
{
	p.ShowPerson();
}
//3、类模板化
template<typename T>
void PrintPerson3(T &p)
{
	p.ShowPerson();
}

int main()
{
	Person<string, int> p1 ("卢本伟", 25) ;
	PrintPerson1(p1);
	PrintPerson2(p1);
	PrintPerson3(p1);

	return 0;
}
typeid(变量名).name()	
可以查看变量的数据类型

2.3 类模板继承

注意事项:

  1. 当父类是类模板时,子类声明时要指定父类中T的类型,否则编译器无法给子类分配内存
template <class T>
class Base
{
	T m;
};

class Son:public Base<int>//需要知道父类中T的类型
{...};
  1. 如果想要灵活指出父类T的类型,子类也需要变为类模板
template <class T>
class Base
{
	T m;
};

template <class T>
class Son :public Base<T>
{
	T obj;
};

如果要更灵活的话:

template <class T>
class Base
{
	T m;
};
template <class T1, class T2>
class Son :public Base<T2>
{
	T1 obj;
};

2.4 类模板成员函数类外实现

template <class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	/*{
		this->m_name = name;
		this->m_age = age;
	}*/
	void showPerson();
	/*{
		cout << this->m_name << this->m_age << endl;
	}*/
	T1 m_name;
	T2 m_age;
};
//构造函数的类外实现
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;
}


int main()
{s

	Person<string, int> p("卢本伟", 25);
	p.showPerson();
	return 0;
}

2.5 类模板友元

全局函数类内实现:直接在类内声明友元即可
全局函数类外实现:需要提前让编译器知道全局函数的存在

//2、全局函数类外实现
//由于需要让编译器提前知道这个函数,需要将函数定义放在最前面
//又由于函数定义中有Person,又要把Person的声明放前面
template<class T1, class T2>
class Person;
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
{
	//1、全局函数类内实现
	friend void printPerson1(Person<T1, T2> p)
	{
		cout << "类内实现:" << p.m_name << p.m_age << endl;
	}

	//2、全局函数类外实现
	//由于类外实现时定义其为模板,所以需要添加空模板参数列表<>
	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;
};



int main()
{
	Person<string, int> p("卢本伟", 25);
	printPerson1(p);
	printPerson2(p);

	return 0;
}

2.6 数组类封装例程

功能:

  1. 可以对内置数据类型以及自定义数据类型的数据进行存储
  2. 将数组中的数据存储到堆区
  3. 构造函数中可以传入数组的容量
  4. 提供对应的拷贝构造函数以及operator-防止浅拷贝问题
  5. 提供尾插法和尾删法对数组中的数据进行增加和删除
  6. 可以通过下标的方式访问数组中的元素
  7. 可以获取数组中当前元素个数和数组的容量

文件1、myarray.hpp,存放自定义的通用数组类

//myarray.hpp
//自定义的通用数组类
#pragma once
#include <iostream>
#include <string>
using namespace std;

template<class T>
class myArray
{
public:
	myArray(int capacity)//构造函数
	{
		cout << "myarray构造函数调用" << endl;
		this->m_capacity = capacity;
		this->m_size = 0;
		this->pAddress = new T[this->m_capacity]; // 按照容量在堆区开辟存放数组的空间
	}
	myArray(const myArray& arr)//拷贝构造函数。防止浅拷贝,浅拷贝会导致析构时堆区数据重复释放
	{
		cout << "myarray拷贝构造函数调用" << endl;
		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_capacity; i++)//将原始数组中的元素一个个拷贝到新开辟的空间------------------m_capacity?课件写的是m_size
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}
	//operator= 赋值运算符重载,为了实现数组的a=b
	myArray& operator=(const myArray& arr)
	{
		cout << "myarray赋值运算符重载调用" << endl;
		//先判断堆区原来是否有数据,有就释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
		}
		//基本同拷贝构造
		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_capacity; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
	}

	// 尾插法实现数据添加
	void Puch_Back(const T& val)
	{
		//判断满了没
		if (this->m_capacity == this->m_size) 
		{
			cout << "塞满了" << endl;
			return;
		}
		this->pAddress[this->m_size] = val;
		this->m_size++;
	}
	// 尾删法实现数据删除。让用户访问不到最后一个元素
	void Pop_Back()
	{
		if (this->m_size == 0)
		{
			cout << "没东西可删" << endl;
			return;
		}
		this->m_size--;
	}
	//通过下标方式访问数组元素
	//因为现在myArray是模板,需要重载中括号。又由于想要通过arr[0] = 100赋值,所以返回的不是T而是T&
	T& operator[](int index)
	{
		return this->pAddress[index];
	}
	//返回容量
	int getCapacity()
	{
		return this->m_capacity;
	}
	//返回大小
	int getSize()
	{
		return this->m_size;
	}


	~myArray()//析构函数
	{
		cout << "myarray析构函数调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress; // 因为是释放数组所以要加[]
			this->pAddress = NULL; //置空防止野指针
		}
	}
private:
	T* pAddress;//指针指向堆区开辟的真实数组
	int m_capacity; // 数组容量
	int m_size; // 数组所占空间大小
};

文件2、test.cpp,用于存放测试代码

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

void printIntArray(myArray <int>&arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i] << endl;
	}
}
void test1() //测试内置数据类型
{
	myArray <int>arr1(15);
	//测试尾插法插入数据
	for (int i = 0; i < 5; i++)
	{
		arr1.Puch_Back(i);
	}
	cout << "arr1的打印输出为:" << endl;
	printIntArray(arr1);

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

	myArray <int>arr2(arr1);
	//测试尾删法删除数据
	arr2.Pop_Back();
	cout << "arr2的打印输出为:" << endl;
	printIntArray(arr2);
	cout << "arr2的容量为:" << arr2.getCapacity() << endl;
	cout << "arr2的大小为:" << arr2.getSize() << endl;


}


class Person
{
public:
	Person() {};//因为在堆区创建,需要弄一个无参空实现
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};
void printPersonArray(myArray<Person>& arr)
{
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i].m_name << arr[i].m_age << endl;
	}
}
void test2() //测试自定义数据类型
{
	myArray<Person> arr(10);
	Person p1("卢本伟", 25);
	Person p2("刀哥", 35);
	Person p3("孙笑川", 30);
	arr.Puch_Back(p1);
	arr.Puch_Back(p2);
	arr.Puch_Back(p3);
	printPersonArray(arr);
}
int main()
{
	test1();
	test2();
	return 0;
}
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值