13—C++模板

01C++提高编程—模板(模板的概念)

#include<iostream>
using namespace std;
#include<string>
/*
本阶段主要针对:
C++泛型编程和STL技术做详细讲解,探讨C++更深层的使用
*/
/*
模板的概念
模板就是建立通用的模具,大大提高复用性

模板的特点:
1. 模板不可以直接使用,它只是一个框架
2.模板的通用并不是万能的
*/

02C++提高编程—模板(函数模板的基本语法)

#include<iostream>
using namespace std;
#include<string>
/*
函数模板
C++另一种编程思想称为:泛型编程,主要利用的技术就是模板

C++提供两种模板机制:
1.函数模板
2.类模板

函数模板语法:
template<typename T>
函数声明或定义

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

函数模板作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表
提高代码的复用性
*/

//1.实现两个整形交换的函数(使用引用的方式传递,实现实参交换)
void swapInt(int &a, int &b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}
//2.实现两个浮点型交换的函数
void swapFloat(float &a, float &b)
{
	float temp;
	temp = a;
	a = b;
	b = temp;
}

//3.创建函数模板
template<typename T>//声明一个模板,让后面的T不报错,T为通用数据类型
//函数的声明和定义
void mySwap(T &a, T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

void Test01()
{
	//不用模板,需要定义两个交换函数
	int a = 10;
	int b = 20;
	//swapInt(a, b);
	float c = 10;
	float d = 20;
	//swapFloat(c, d);

	//下面使用函数交换的模板
	//使用模板有两种方式:1.自动类型推倒,2.显示指定类型
	//1.自动类型推倒
	mySwap(a, b);
	cout << "a=" << a << "   b=" << b << endl;
	//2.显示指定类型
	mySwap<float>(c, d);
	cout << "c=" << c << "   d=" << d << endl;
}
int main()
{
	Test01();
	system("pause");
	return 0;
}

03C++提高编程—模板(函数模板的注意事项)

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

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

//1.自动类型推导,必须推导出一致的数据类型T,才可以使用
template<class T>
void mySwap(T &a,T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

//2.模板必须要确定出T的数据类型,才可以使用
template<class T>
void func()
{
	cout << "func被调用" << endl;
}

void Test01()
{
	int a = 10;
	int b = 20;
	char c = 'c';
	mySwap(a, b);
	cout << "a=" << a << "   b=" << b << endl;
	//mySwap(a, c);//自动类型推导,数据类型不一致,报错
}

void Test02()
{
	//模板必须要确定出T的数据类型,这里没有也必须指定(随便指定一个),否则报错
	//func();
	func<int>();
}

int main()
{
	Test01();
	Test02();
	system("pause");
	return 0;
}

04C++提高编程—模板(小案例)

#include<iostream>
using namespace std;
#include<string>
/*
案例描述:
1.利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
2.排序规则从大到小,排序算法为选择排序
3.分别利用char数组和int数组进行测试

选择排序:
第一次从待排序的数据元素中选出最小(或最大)的一个元素,
存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,
然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零
*/

//1.定义一个两个元素交换的函数模板
template<class T>
void mySwap(T &a,T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}
//2.定义一个数组排序的函数模板(sort:排序)
template<class T>
void mySort(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		T max = i;//定义最大值得下标
		for (int j = i + 1; j < len; j++)//让第一个数和后面的比较
		{
			if (arr[max] < arr[j])
			{
				max = j;//交换下标,是max拿到最大值得下标
			}
		}
		if (max != i)//把最大值放在第一位
		{
			mySwap(arr[i], arr[max]);
		}
	}
}
//打印数组模板函数
template<class T>
void myPrint(T arr[],int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "arr[" << i << "]=" << arr[i] << endl;
	}
}

void test01()
{
	//测试char数组
	char charArr[] = "bdcfeagh";
	int num = sizeof(charArr) / sizeof(char);
	mySort<char>(charArr, num);//显示
	myPrint(charArr, num);//自动类型推导
}

void test02()
{
	//测试int数组
	int intArr[] = {1,7,9,8,5,3,6,7,9,5};
	int num = sizeof(intArr)/sizeof(int);
	mySort(intArr, num);
	myPrint(intArr, num);
}

int main()
{
	test01();
	cout << "-------------" << endl;
	test02();
	system("pause");
	return 0;
}

05C++提高编程—模板(普通函数和模板的区别)

#include<iostream>
using namespace std;
#include<string>
/*
普通函数与函数模板区别:
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';

	cout << myAdd01(a, c) << endl; //正确,将char类型的'c'隐式转换为int类型  'c' 对应 ASCII码 99
	
	//myAdd02(a, c); // 报错,使用自动类型推导时,不会发生隐式类型转换

	myAdd02<int>(a, c); //正确,如果用显示指定类型,可以发生隐式类型转换
	cout << myAdd02<int>(a, c) << endl;
}

int main() {
	test01();
	system("pause");
	return 0;
}

06C++提高编程—模板(普通函数和模板调用规则)

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

总结:既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性
*/

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

template<typename T>
void myPrint(T a, T b)
{
	cout << "调用的模板" << endl;
}

template<typename T>
void myPrint(T a, T b, T c)
{
	cout << "调用重载的模板" << endl;
}

void test01()
{
	//1、如果函数模板和普通函数都可以实现,优先调用普通函数
	// 注意 如果告诉编译器,普通函数是有的,但只是声明没有实现,或者不在当前文件内实现,就会报错找不到
	int a = 10;
	int b = 20;
	myPrint(a, b); //调用普通函数

	//2、可以通过空模板参数列表来强制调用函数模板
	myPrint<>(a, b); //调用函数模板

	//3、函数模板也可以发生重载
	int c = 30;
	myPrint(a, b, c); //调用重载的函数模板

	//4、如果函数模板可以产生更好的匹配,优先调用函数模板
	//因为普通函数提供的是整型的数据,函数模板可以自动匹配出字符型数据
	char c1 = 'a';
	char c2 = 'b';
	myPrint(c1, c2); //调用函数模板
}
int main() 
{
	test01();
	system("pause");
	return 0;
}

07C++提高编程—模板(模板的局限性)

#include<iostream>
using namespace std;
#include<string>
/*
模板的局限性:模板的通用性并不是万能的

例如:
template<class T>
void f(T a, T b)
{
a = b;
}
上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

例如:
template<class T>
void f(T a, T b)
{
if(a > b) { ... }
}
在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行

因此C++为了解决这种问题,提供模板的重载,可以为这些,特定的类型,提供具体化的模板

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

*/

//定义一个类
class Person
{
public:
	//定义有参构造
	Person(string name,int age)
	{
		//那个对象调用有参构造,this指针就代表那个对象实体,然后把传过来的参数赋值给对象的属性
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
//普通模板
template<class T>
bool myCompare(T &a,T &b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型
//具体化优先于常规模板
template<> bool myCompare(Person &p1, Person &p2)
{
	if (p1.m_Age == p2.m_Age&&p1.m_Name == p2.m_Name)
	{
		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);
	//自定义数据类型,不会调用普通的函数模板
	//可以创建具体化的Person数据类型的模板,用于特殊处理这个类型
	bool ret = myCompare(p1, p2);
	if (ret)
	{
		cout << "p1 == p2 " << endl;
	}
	else
	{
		cout << "p1 != p2 " << endl;
	}
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

08C++提高编程—模板(类模板)

#include<iostream>
using namespace std;
#include<string>
/*
类模板作用:
建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表。

语法:
template<typename T>
类

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

类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板,加函数就为函数模板
*/


//类模板
//通用化两个类型,需要用两个代表
template<class NameType, class AgeType>
class Person
{
public:
	Person(NameType name, AgeType age)
	{
		this->mName = name;
		this->mAge = age;
	}
	void showPerson()
	{
		cout << "name: " << this->mName << " age: " << this->mAge << endl;
	}
public:
	NameType mName;
	AgeType mAge;
};

void test01()
{
	// 指定NameType 为string类型,AgeType 为 int类型
	//显示指定数据类型
	Person<string, int>P1("AISMALL", 18);
	P1.showPerson();
}

int main() {
	test01();
	system("pause");
	return 0;
}

09C++提高编程—模板(类模板和函数模板的区别)

#include<iostream>
using namespace std;
#include<string>
//类模板和函数模板的区别
/*
类模板与函数模板区别主要有两点:
1. 类模板没有自动类型推导的使用方式
2. 类模板在模板参数列表中可以有默认参数
*/

//创建类模板
//我们在创建模板时指定默认参数,在后面就可以省略了(类似于自动推导一样)
//在函数模板中不允许使用默认参数
template <class nameType,class ageType=int>
class Person
{
public:
	Person(nameType name,ageType age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	void show()
	{
		cout << "m_Age=" << this->m_Age << " m_Name=" << this->m_Name << endl;
	}
private:
	nameType m_Name;
	ageType m_Age;
};

void test01()
{
	Person<string> p1("AISMALL", 18);
	p1.show();
}
int main() {
	test01();
	system("pause");
	return 0;
}

10C++提高编程—模板(类模板中成员函数创建时机)

#include<iostream>
using namespace std;
#include<string>
/*
类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:
1.普通类中的成员函数一开始就可以创建
2.类模板中的成员函数在调用时才创建
*/
class Person1
{
public:
	void showPerson1()
	{
		cout << "Person1 show" << endl;
	}
};

class Person2
{
public:
	void showPerson2()
	{
		cout << "Person2 show" << endl;
	}
};

template<class T>
class MyClass
{
public:
	T obj;
	//类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成
	/*如果不是调用是在创建成员函数,
	这种方法比编译通不过,因为obj的类型无法判定,报错*/
	void fun1() { obj.showPerson1(); }
	void fun2() { obj.showPerson2(); }
};

void test01()
{
	//相当于Person1 m<==>Person1 obj
	MyClass<Person1> m;
	m.fun1();

	//没有创建Person2的实例,func2不被调用,因为Person1的实例无法调用func2
	//m.fun2();//编译会出错,说明函数调用才会去创建成员函数
}

int main() 
{
	test01();
	system("pause");
	return 0;
}

11C++编程提高—模板(类模板对象做函数参数)

#include<iostream>
using namespace std;
#include<string>
/*
类模板对象做函数参数:
类模板实例化出的对象,做为函数参数进行传递

一共有三种传入方式:
1. 指定传入的类型   --- 直接显示对象的数据类型(最常用)
2. 参数模板化       --- 将对象中的参数变为模板进行传递
3. 整个类模板化     --- 将这个对象类型 模板化进行传递
*/

/*
运行结果:
T3的类型为:string 
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
T4的类型为:int
int
T的类型为:preson(string,int)
class Person<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>
*/

//类模板
template<class T1, class T2= int>
class Person
{
public:
	Person(T1 name, T2 age)
	{
		this->mName = name;
		this->mAge = age;
	}
	void showPerson()
	{
		cout << "name: " << this->mName << " age: " << this->mAge << endl;
	}
public:
	T1 mName;
	T2 mAge;
};
//1、指定传入的类型:Person &p=p,使用引用的方式进行传递
void printPerson1(Person<string, int> &p)
{
	p.showPerson();
}
void test01()
{
	Person <string, int >p("AISMALL_01", 18);
	printPerson1(p);
}

//2、参数模板化(告诉编译器T3,T4也是模板)
template <class T3, class T4>
void printPerson2(Person<T3, T4>&p)
{
	p.showPerson();
	cout << "T3的类型为: " << typeid(T3).name() << endl;
	cout << "T4的类型为: " << typeid(T4).name() << endl;
}
void test02()
{
	Person <string, int >p("AISMALL_02", 19);
	printPerson2(p);
}

//3、整个类模板化
template<class T>
void printPerson3(T & p)
{
	cout << "T的类型为: " << typeid(T).name() << endl;
	p.showPerson();

}
void test03()
{
	Person <string, int >p("AISMALL_03", 20);
	printPerson3(p);
}

int main() {
	test01();
	test02();
	test03();
	system("pause");
	return 0;
}

12C++编程提高—模板(类模板与继承)

#include<iostream>
using namespace std;
#include<string>
/*
类模板与继承:

当类模板碰到继承时,需要注意一下几点:
1.当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
2.如果不指定,编译器无法给子类分配内存
3.如果想灵活指定出父类中T的类型,子类也需变为类模板
*/
template<class T>//定义一个模板
class Base//基类
{
	T m;
};

//1.错误,c++编译需要给子类分配内存,必须知道父类中T的类型才可以向下继承
//class Son:public Base 

class Son :public Base<int> //必须指定一个类型
{
};
void test01()
{
	Son s1;
}

//3.类模板继承类模板 ,可以用T2指定父类中的T类型
template<class T1, class T2>
//template<class T2>
class Son2 :public Base<T2>
{
public:
	//Son2构造函数
	Son2()
	{
		cout << typeid(T1).name() << endl;//int
		cout << typeid(T2).name() << endl;//char
	}
};

void test02()
{
	//Son2调用构造函数(由于模板是两个参数,所以实例化时必须指定两个参数)
	//T1=int,T=T2=char
	Son2<int, char> s2;

	//Son2<char> s2;
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

13C++编程提高—模板(类模板成员函数类外实现)

#include<iostream>
using namespace std;
#include<string>
//注意:类模板中成员函数类外实现时,需要加上模板参数列表

//类模板中成员函数类外实现
template<class T1, class T2>
class Person {
public:
	//成员函数类内声明
	Person(T1 name, T2 age);
	void showPerson();

public:
	T1 m_Name;
	T2 m_Age;
};

//构造函数 类外实现,模板参数列表:<T1, T2>
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;
}

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

int main() {
	test01();
	system("pause");
	return 0;
}

14C++编程提高—模板(类模板的分文件编写)

普通类头文件:14cat.h

//类声明头文件
#pragma once
#include <iostream>
using namespace std;
#include <string>

class Cat
{
public:
	Cat(string name, int age);
	void showAnimal();
public:
	string m_Name;
	int m_Age;
};

模板类头文件:14person.h

//类声明头文件
#pragma once
#include <iostream>
using namespace std;
#include <string>

template<class T1, class T2>
class Person {
public:
	Person(T1 name, T2 age);
	void showPerson();
public:
	T1 m_Name;
	T2 m_Age;
};


普通类的实现(源文件):14cat.cpp

#include "14cat.h"
//类实现分文件(要包含头文件)

Cat::Cat(string name, int age)
{
	this->m_Name = name;
	this->m_Age = age;
}
void Cat::showAnimal() {
	cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
}

模板类的实现(源文件):14person.cpp

#include "14person.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>::showPerson() {
	cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
}

主函数(源文件)

#include<iostream>
using namespace std;
#include<string>
/*
分文件编写:
把类声明和实现都放在同一个头文件中,或者把类的声明和实现,一个放在头文件中,一个放在源文件中

类模板分文件编写

掌握类模板成员函数分文件编写产生的问题以及解决方式

问题:类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到

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

/*
1.第一种:模板类采用类内声明类外定义,而且还不在一个文件中
如果只包含以.h头文件,因为类模板中成员函数创建时机是在调用阶段,
在编译的时候只能找到.h文件,无法找到模板类外实现的成员函数
(类的成员函数,采用类内声明类外定义的方式),
这个时候只要包含类外实现的成员函数的文件即可(其实只包含这个文件也可以),
但是只包含类模板.h文件不行
*/

/*
2.第二种:模板类依然采用类内声明,类外定义,声明和实现在一个文件中,后缀名为.hpp

注意:无论是第一种还是第二种,对普通的分文件编写都是可以的,因为普通类成员函数一开始就可以创建,
*/
 
#include"14person.h"
#include"14cat.h"
//#include "14person.cpp" //解决方式1,包含cpp源文件

//解决方式2,将成员函数声明和实现写到一起,文件后缀名改为.hpp(常用),也可以为h
//此处不做演示

void test01()
{
	//普通类,只包含声明类的头文件即可(类的成员函数的声明采用类内声明类外定义的方式)
	//验证普通类
	Cat c1("Tom", 2);
	c1.showAnimal();
	//模板类(需要包含两个文件才可以)
	//Person<string, int> p("Tom", 10);
	//p.showPerson();
}

int main() {
	test01();
	system("pause");
	return 0;
}

15C++提高编程—模板(类模板与友元)

#include<iostream>
using namespace std;
#include<string>
/*
全局函数类内实现 - 直接在类内声明友元即可(真香)

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

建议全局函数做类内实现,用法简单,而且编译器可以直接识别
*/

//2.3提前让编译器知道Person类的存在
template<class T1,class T2>
class Person;

//2.2全局函数类外实现(为了编译器提前知道,写在上方,不然还要声明)
template<class T1, class T2>
void showPerson02(Person<T1, T2> &p)
{
	cout << "类外实现 ---- 年龄: " << p.m_Age << " 姓名:" << p.m_Name << endl;
}


//创建模板类(类中有两个私有变量)
template < class T1, class T2 > 
class Person
{
	//1.1全局函数类内实现(访问类内私有属性加友元)
	friend void showPerson(Person<T1, T2> &p)
	{
		cout << "类外实现 ----年龄:" << p.m_Age << "  姓名:" << p.m_Name << endl;
	}

	//2.1全局函数的类外实现(访问类内私有属性加友元)
	//由于下方使用了模板,这里需要加空的参数列表<>
	friend void showPerson02<>(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;
};


//1.2全局函数类内实现
void test01()
{
	Person<string, int> p("AISMALL", 18);
	//根据调用方式可以区别全局函数和成员函数
	//成员函数的调用方法是对象加点的方式,全局函数是使用函数名来调用
	showPerson(p);
}

//2.4全局函数类外实现
void test02()
{
	Person<string, int> p("AISMALL_02", 18);
	//根据调用方式可以区别全局函数和成员函数
	//成员函数的调用方法是对象加点的方式,全局函数是使用函数名来调用
	showPerson02(p);
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

16C++提高—模板(案例)

头文件: 16myArray

//采用分文件编写
/*
自己的数组类,由于采用模板进行类的编写,所以声明和实现写在一起,
因为模板类的成员函数是调用的时候才创建的(前面讨论过这个问题)
*/
#pragma once
#include<iostream>
using namespace std;

//模板类
template<class T>
class myArray
{
public:
	//有参构造 参数:容量(size为数组当前存的个数,不能大与capacity)
	//可以不存满,但绝对不能存多
	myArray(int capacity)
	{
		cout << "有参构造调用" << endl;
		this->m_Capacity = capacity;
		this->m_Size = 0;//初始化数组大小
		//pAddress为指针,接受地址使用,new关键字返回的是地址
		//new T[this->m_Capacity]<=对比=>new int[10]
		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]
	//重载[] 操作符  arr[0]
	T& operator [](int index)
	{
		return this->pAddress[index]; //不考虑越界,用户自己去处理
	}

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

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


	//析构函数(因为堆区的数据要手动释放,默认析构不行)
	~myArray()
	{
		cout << "析构函数调用" << endl;
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;//由于数数组要加中括号
			this->pAddress = NULL;//指向空,防止出现野指针
		}
	}


	//拷贝构造(因为堆区有数据会涉及深浅拷贝,默认拷贝不行)
	myArray(const myArray &arr)
	{
		cout << "拷贝构造调用" << endl;
		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& arr)
	{
		cout << "operator=调用" << endl;
	//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Size = 0;
			this->m_Capacity = 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;//返回自身,使用引用接受(链式编程)
	}

	//成员变量,只有一个是不确定类型的,使用模板代替
private:
	T *pAddress;//指针指向堆区开辟的真时数组
	int m_Capacity;//数组的容量
	int m_Size;//数组大小
};

源文件:

#include<iostream>
using namespace std;
#include<string>
/*
案例描述:  实现一个通用的数组类,要求如下:
1.可以对内置数据类型以及自定义数据类型的数据进行存储
2.将数组中的数据存储到堆区(数据在堆区:注意深浅拷贝问题)
3.构造函数中可以传入数组的容量
4.提供对应的拷贝构造函数以及operator=防止浅拷贝问题
5.提供尾插法和尾删法对数组中的数据进行增加和删除
6.可以通过下标的方式访问数组中的元素
7.可以获取数组中当前元素个数和数组的容量

注意:通用数组也是数组,存放的值得类型必须相同,
*/

//包含头文件
//注意:普通头文件使用h后缀,模板类头文件使用hpp(就是一种约定,不是必须)
#include"16myArray.hpp"

void test01()
{
	//模板类实例化要指定类型
	myArray<int> arr1(5);//测试有参和析构
	myArray<int> arr2(arr1);//测试拷贝
	myArray<int> arr3(100);
	arr3 = arr1;//测试operator=
}

//打印函数
void printIntArray(myArray<int>& arr) {
	for (int i = 0; i < arr.getSize(); i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}

//测试内置数据类型
void test02()
{
	myArray<int> array1(10);
	for (int i = 0; i < 10; i++)
	{
		array1.Push_back(i);//尾插法初始化数组
	}
	cout << "array1打印输出:" << endl;
	printIntArray(array1);//调用打印函数打印数组
	cout << "array1的大小:" << array1.getSize() << endl;//10
	cout << "array1的容量:" << array1.getCapacity() << endl;//10

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

	myArray<int> array2(array1);
	array2.Pop_back();//调用尾删方法,是size值减小1
	cout << "array2打印输出:" << endl;
	printIntArray(array2);//打印数组,但是最后一个值被屏蔽了
	cout << "array2的大小:" << array2.getSize() << endl;//9
	cout << "array2的容量:" << array2.getCapacity() << endl;//10
}

//测试自定义数据类型
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 test03()
{
	//创建数组
	myArray<Person> pArray(10);//容量为10
	Person p1("凯", 26);
	Person p2("赵云", 26);
	Person p3("兰陵王", 26);
	Person p4("韩信", 26);
	Person p5("典韦", 26);

	//插入数据
	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;//5
	cout << "pArray的容量:" << pArray.getCapacity() << endl;//10

}

int main() {
	//test01();
	test02();
	test03();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彤彤的小跟班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值