【基础】【二】【模板】C++学习日记

DAY 10

模板

建立通用的模具,提高复用性

函数模板

语法:

template<typename T>
函数

解释:
template — 声明创建模板
typename — 表明其后面的符号是一种数据类型,可以用class代替
T — 通用的数据类型,名称可以替换,通常为大写字母
例如:

// 函数模板
// 声明一个模板,告诉编译器后面代码紧跟的T不要报错
template<typename T>
void swap(T &a, T &b)
{
	T temp = a;
	a = b;
	b = temp;
}
int main()
{
	int a = 10, b = 20;
	// 使用函数模板
	// 1、自动类型推导
	swap(a, b);
	// 2、显式指定类型
	swap<int>(a, b);
	return 0;
}

总结:
     1、函数模板利用关键字template
     2、使用函数模板有两种方式:自动类型推导,显式指定类型
     3、模板的目的是为了提高复用性,将类型参数化

函数模板注意事项

注意事项:
     1、自动类型推导,必须推导出一致的数据类型T,才可以使用
     2、模板必须要确定出T的数据类型,才可以使用

template<typename T>
void swap(T &a, T &b)
{
	T temp = a;
	a = b;
	b = temp;
}
template<typename T>
void func()
{
	
}
int main()
{
	// 1、
	int a = 10, int b = 20;
	char c = 'c';
	swap(a, b); // 正确
	swap(a, c); // 错误
	// 2、
	func(); // 错误
	func<int>(); // 正确
}
普通函数和函数模板的区别

区别:
    1、普通函数调用时可以发生自动类型转换(隐式类型转换)
    2、函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
    3、如果利用显示指定类型的方式,可以发生隐式类型转换
函数模板建议使用显示指定类型

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

调用规则:
    1、如果函数模板和普通函数都可以实现,优先调用普通函数
    2、可以通过空模板参数列表来强制调用函数模板
    3、函数模板也可以发生重载
    4、如果函数模板可以产生更好的匹配,优先调用函数模板
普通函数与模板函数最好不要一起出现,防止出现二义性

模板机制

1、编译器并不是把函数模板处理成能够处理任何类型的函数
2、函数模板通过具体类型产生不同的函数
3、编译器会对函数模板进行两次编译,在声明的地方对模板代码本身进行编译,在调用的地方对参数替换后的代码进行编译

模板的局限性

对于一些特定的数据类型,模板不能使用,例:

#include<iostream>
using namespace std;
#include<string>
class  Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
template<class T>
bool isEqual(T a, T b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}
// 利用具体化Person的版本实现代码,具体优先调用
template<> bool isEqual(Person p1, Person p2)
{
	if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	int a = 10, b = 10;
	if (isEqual(a, b))
	{
		cout << "a == b" << endl;
	}
	else
	{
		cout << "a != b" << endl;
	}
	Person p1("张三", 18);
	Person p2("张三", 18);
	if (isEqual(p1, p2))
	{
		cout << "p1 == p2" << endl;
	}
	else
	{
		cout << "p1 != p2" << endl;
	}
	return 0;
}

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

类模板

建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代表
语法:

template<typename T>
#include<iostream>
using namespace std;
#include<string>
template<typename TypeName, typename TypeAge>
class  Person
{
public:
	Person(TypeName name, TypeAge age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void show()
	{
		cout << "姓名为:" << this->m_Name << "  年龄为:" << this->m_Age << endl;
	}
	TypeName m_Name;
	TypeAge m_Age;
};

int main()
{
	Person<string, int> p("张三", 18);
	p.show();
	return 0;
}
类模板和函数模板的区别

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

#include<iostream>
using namespace std;
#include<string>
template<typename TypeName, typename TypeAge = int>
class  Person
{
public:
	Person(TypeName name, TypeAge age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void show()
	{
		cout << "姓名为:" << this->m_Name << "  年龄为:" << this->m_Age << endl;
	}
	TypeName m_Name;
	TypeAge m_Age;
};
int main()
{
	Person<string> p("张三", 18);
	p.show();
	return 0;
}
类模板中成员函数创建时机

普通类中成员函数一开始就可以创建
类模板中的成员函数调用时才创建

#include<iostream>
using namespace std;
#include<string>
class  Person1
{
public:
	void show1()
	{
		cout << "Person1的show1()"<< endl;
	}
};
class Person2
{
public:
	void show2()
	{
		cout << "Person2的show2()" << endl;
	}
};
template<class T>
class Person
{
public:
	T obj;
	void show1()
	{
		obj.show1();
	}
	void show()
	{
		obj.show2();
	}
};
int main()
{
	Person<Person1> p;
	p.obj.show1();
	// p.obj.show2(); // 出错
	return 0;
}
类模板对象做函数参数

传入方式:
    1、指定传入类型 — 直接显示对象的数据类型
    2、参数模板化 — 将对象中的参数变为模板进行传递
    3、整个类模板化 — 将这个对象类型模板化进行传递

#include<iostream>
using namespace std;
#include<string>
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;
};
// 1、指定传入类型
void test01(Person<string, int>&p)
{
	p.showPerson();
}
// 2、参数模板化
template<class T1, class T2>
void test02(Person<T1, T2>&p)
{
	p.showPerson();
}
// 3、整个类模板化
template<class T>
void test03(T& p)
{
	p.showPerson();
}
int main()
{
	// 1、指定传入类型
	Person<string, int> p1("张三", 20);
	test01(p1);
	// 2、参数模板化
	Person<string, int> p2("李四", 22);
	test02(p2);
	// 3、整个类模板化
	Person<string, int> p3("赵五", 24);
	test03(p3);
	return 0;
}
类模板与继承

注意:
    1、当子类继承的父类是一个类模板时,子类在声明的时候,要指出父类中的类型
    2、如果不指定,编译器无法给子类分配内存
    3、如果想灵活指出父类中T的类型,子类也要变为类模板

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;
};
// 子类继承要指出父类模板的类型,否则不能继承,因为不知道类型大小
// 无法分配内存
class Son :public Person<string, int>
{

};
// 想灵活指定父类中T类型,子类也需要模板
template<class T1, class T2>
class Son2 :public Person<T1, T2>
{
	T1 m_Name;
};
类模板成员函数类外实现
template<class T1, class T2>
class Person
{
public:
	Person(T1 name, T2 age);
	void showPerson();
	
	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;
}
类模板分文件编写

问题:类模板中成员函数创建时机是在调用阶段,导致文件编写时链接不到
解决:
    1、直接包含.cpp源文件
    2、将声明和实现写到同一个文件中,并更改后缀为.hpp,hpp是约定的名称,并不是强制

类模板和友元

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

#include<iostream>
using namespace std;
#include<string>
// 类的定义
template<class T1, class T2>
class Person;
// 全局函数类外实现
template<class T1, class T2>
void show2(Person<T1, T2> p)
{
	cout << "姓名:" << p.m_Name << "  年龄:" << p.m_Age << endl;
}
// 类的实现
template<class T1, class T2>
class Person
{
	// 全局函数类内实现
	friend void show(Person<T1, T2> p)
	{
		cout << "姓名:" << p.m_Name << "  年龄:" << p.m_Age << endl;
	}
	// 全局函数类外实现
	// 加空模板的参数列表
	// 需要提前让编译器知道这个函数的存在,放在类实现前面
	friend void show2<>(Person<T1, T2> p);

public:
	Person(T1 name, T2 age);
	
private:
	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;
}
int main()
{
	Person<string, int>p1("张三", 18);
	show(p1);
	Person<string, int>p2("李四", 18);
	show2(p2);
	return 0;
}

使用类模板实现数组类封装

模板类数组的定义与实现

#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[capacity];
	}
	MyArray(const MyArray& arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[this->m_Capacity];
		for (int i = 0; i < this->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[this->m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}
		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()
	{
		if (this->m_Size == 0)
		{
			return;
		}
		this->m_Size--;
	}
	T& operator[](int index)
	{
		return this->pAddress[index];
	}
	int capacity()
	{
		return this->m_Capacity;
	}
	int size()
	{
		return this->m_Size;
	}
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[]this->pAddress;
		}
	}
private:
	T* pAddress;
	int m_Capacity;
	int m_Size;
};

使用自定义的数组类

#include "myarray.hpp"
#include<string>
class Person
{
public:
	Person() {};
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

void print(MyArray<int>&arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		cout << arr[i];
	}
	cout << endl;
}
void print(MyArray<Person>& arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		cout << "姓名:" << arr[i].m_Name << "  年龄:" << arr[i].m_Age;
	}
	cout << endl;
}
void test01()
{
	MyArray<int>arr(5);
	for (int i = 0; i < 5; i++)
	{
		arr.push_back(i);
	}
	print(arr);
	MyArray<int>arr1(arr);
	print(arr1);
	MyArray<int>arr2(100);
	print(arr2);
	arr2 = arr;
	arr2.pop();
	print(arr2);
}
void test02()
{
	MyArray<Person>arr(10);
	arr.push_back(Person("孙悟空", 20));
	print(arr);
}
int main()
{
	test01();
	test02();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值