模板
1.模板
1.1 模板的概念
建立通用的模具,提高复用性。比如PPT模板。
特点:
- 模板不能直接使用,他只是一个框架
- 模板是通用的,但是并不是万能的。
1.2 函数模板
C++的另一种编程思想称为泛型编程,利用的主要技术就是模板。C++提供两种模板机制:函数模板和类模板。
1.2.1 函数模板语法
函数模板的作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体指定,用一个虚拟的类型来代表(在使用的时候才会确定类型)。
语法:
template<typename T> // 告诉编译器开始写模板
// 然后写函数声明或定义
template
:声明创建模板
typename
:声明其后面的符号是一种数据类型,可以用class代替
T
:通用数据类型,名称可以替换,通常为大写字母
示例:
#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不要报错,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);
// 利用模板交换数据
// 方法一 --- 自动类型推导
mySwap(a,b);
// 因为a和b的定义是int,在传参的时候,编译器推测出T为int类型的数据
// 方法二 --- 显示指定类型
mySwap<int>(a,b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
double c = 1.1;
double d = 2.2;
//swapDouble(c,d);
mySwap<double>(c,d);
cout << "c: " << c << endl;
cout << "d: " << d << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
按照以前的方法,每个类型的数据进行交换的时候,都要写一个交换函数。除了内置数据类型,还有其他各种自定义数据类型,所以对每个类型写数据交换函数是很繁琐的。不难看出,不管是什么类型的数据进行交换,代码基本相同,只是对应的数据类型不同。所以这里就用模板的方式来实现各种数据类型的交换操作。函数模板的使用有自动类型推导和显式指定类型两种方式。模板的使用提高了代码的复用性,将类型参数化。
1.2.2 函数模板注意事项
- 自动类型推导,必须推导出一致的数据类型T才可以使用。
- 模板必须确定出T的数据类型,才可以使用。
示例:
#include<iostream>
using namespace std;
// === 函数模板注意事项 ===
// 函数模板
template<class T>
void mySwap(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
};
// 推导出一致的数据类型
void test01()
{
int a = 10;
int b = 20;
// char c = "c";
// mySwap(a,b); // 正确
// mySwap(a,c); // 错误!推导不出一致的数据类型
cout << "a: " << a << endl;
cout << "b: " << b << endl;
}
// 必须确定出T的数据类型
template<class T>
void func()
{
cout << "func的调用" <<endl;
}
void test02()
{
// func(); // 没有确定(无法自动推导)T的类型,无法使用函数func
func<int>(); // 随便定义一个,就可以调用函数func
}
1.2.3 函数模板的案例
案例描述:
- 利用函数模板封装一个排序函数,可以对不同数据类型数组进行排序。
- 排序规则从大到小,排序算法为选择排序。
- 分别利用 char 数组和 int 数组进行测试。
示例:
#include<iostream>
using namespace std;
// 交换模板
template<typename T>
void mySwap(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
// 排序算法---选择排序
template<typename T>
void mySort(T arr[],int len)
{
for(int i = 0; i<len; i++)
{
int max = i; // max为最大值下标,初始认定第一个元素为最大值
// 寻找真正最大值
for(int j = i+1; j<len; j++)
// 这里是遍历了所有的元素,找到的就是最大值
{
// 遍历到的值如果大于初始认定值,遍历值才是最大值
if(arr[max]<arr[j])
{
max = j; // 更新最大值
}
}
if(max != i)
{
// 交换max和i元素
mySwap(arr[max],arr[i]);
}
}
}
// 打印数组的模板
template<typename T>
void printArray(T arr[],int len)
{
for(int i = 0;i<len;i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void test01()
{
char charArr[] = "fdegbac";
int num = sizeof(charArr)/sizeof(char);
mySort(charArr,num); //自动类型推导
printArray(charArr,num);
}
void test02()
{
int intArr[] = {2,6,1,3,4,7,5};
int num = sizeof(intArr)/sizeof(int);
mySort(intArr,num);
printArray(intArr,num);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
执行结果如下:
1.2.4 普通函数与函数模板的区别
- 普通函数调用时可以发生自动类型转换(隐式类型转换)。
- 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换;如果利用显示指定类型的方式,则可以发生隐式类型转换。
// 普通函数
int myIntAdd(int a,int b)
{
return a+b;
}
// 函数模板
template<typename T>
T myIntAdd1(T a,T b)
{
return a+b;
}
void test01()
{
int a = 10;
int b = 20;
cout << myIntAdd(a,b)<< endl;
char c = 'c'; // ASCII a - 97 c - 99
cout << myIntAdd(a,c)<< endl;
// 函数可以执行,结果是109,因为发生了隐式类型转换
// 把字符c的ACSII码换成int类型的数值就是99
cout << myIntAdd1(a,b)<< endl;
// 自动类型推导
//cout << myIntAdd1(a,c)<< endl;
// 无法执行,不能推出统一的类型,不会发生自动类型转换
// 显示指示类型
cout << myIntAdd1<int>(a,c)<< endl;
// 可以发生隐式类型转换
}
没有指明类型是的编译报错情况如下:
一般使用显示指定类型来调用模板,因为编译器猜的不一定正确。
1.2.5 普通函数和函数模板的调用规则
普通函数和函数模板可以同名,即发生重载。在重载的情况下,两个函数的调用规则如下:
- 如果函数模板和普通函数都可以实现,优先调用普通函数;
- 可以通过控制空模板参数列表来强制调用函数模板;
- 函数模板也可以发生重载
- 如果函数模板可以产生更好的匹配,优先调用函数模板
(实际开发中两者一般不会同时出现)
#include<iostream>
using namespace std;
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;
// 优先调用普通函数
myPrint(a,b);
// 空模板参数列表,强制调用函数模板
myPrint<>(a,b);
// 重载
myPrint(a,b,100);
// 更好的匹配,优先调用模板
char c1 = 'a';
char c2 = 'b';
myPrint(c1,c2); // 函数模板直接推导出为char,而不用进行隐式类型转换,所以会调用函数模板
}
int main()
{
test01();
system("pause");
return 0;
}
1.2.6 模板的局限性
模板的通用性并不是万能的。
对于下面的代码,如果传入的是数组,就不能进行赋值操作。
template<class T>
void f(T a,T b)
{
a = b;
}
再比如下面的一段代码,根据比较的结果,执行对应内容,如果传的是自定义数据类型,也是无法正常运行。
template<class T>
void f(T a,T b)
{
If(a > b) {…}
}
C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板。
#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 myCompare(T &a,T &b)
{
if(a == b)
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;
}
// 利用具体化的Person版本来实现,会优先调用
template<> bool myCompare(Person &p1,Person &p2) // template<> 表示的是模板的重载
{
if(p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age)
return true;
else
return false;
}
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();
system("pause");
return 0;
}
学习模板并不是为了写模板,而是在STL中能够运用系统提供的模板。
1.3 类模板
1.3.1 类模板的语法
建立一个通用类,类中的成员数据类型可以不具体指定,用一个虚拟的类型来代表。
基本格式:
template<typename T>
// 开始写类
示例
#include<iostream>
using namespace std;
#include<string>
// 类模板
template<class NameType,class AgeType>
class Person
{
public:
Person(NameType name,AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "Name: " << m_Name << endl;
cout << "Age: " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test01()
{
Person<string,int> p1("张大哥",19); // Person p1();
// 类型参数化
p1.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
1.3.2 类模板与函数模板的区别
- 类模板没有自动类型推导的使用方式
- 类模板在模板参数列表中可以有默认参数
#include<iostream>
using namespace std;
// 类模板
// template<class NameType,class AgeType>
template<class NameType,class AgeType = int>
class Person
{
public:
Person(NameType name,AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "Name: " << m_Name << endl;
cout << "Age: " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
// 1. 类模板没有自动类型推导的使用方式
void test01()
{
// Person p1("孙悟空",100); //错误!无法使用自动类型推导
Person<string,int> p1("孙悟空",999); //正确
}
// 2. 类模板在模板参数列表中可以有默认参数
void test02()
{
Person<string> p1("猪八戒",999);
p1.showPerson();
}
int main()
{
test02();
system("pause");
return 0;
}
有默认参数的时候,创建类的时候可以不用给定数据类型。如果给定了,就按照给定的类型,如果没有就按照默认参数类型。
1.3.3 类模板中成员函数的创建时机
普通类中的成员函数一开始就可以创建,而类模板中的成员函数在调用时才创建。
//类模板中的成员函数在调用时才创建
#include<iostream>
using namespace std;
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;
// 类模板中的成员函数
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
void test01()
{
MyClass<Person1>m;
m.func1();
// m.func2();
}
int main()
{
test01();
system("pause");
return 0;
}
函数test01()
为空实现时候,程序是可以编译通过的(此时没有类模板成员函数的调用)。这是因为模板类中的成员函数没有创建,因为T的类型还不确定。
在test01中用MyClass<Person1> m
去创建一个对象的时候,这个时候确定了T的类型是Person1,然后去调用func1和func2的时候会出现错误:
[Error] 'class Person1' has no member named 'showPerson2'; did you mean 'showPerson1'? 。
如果只是调用func1,程序也不会报错,同理,创建一个Perosn2类型的对象也是如此。所以类模板中的成员函数是在调用时才创建的。
1.3.4 类模板对象做函数参数
使用类模板实例化出的对象,作为参数向函数传递的时候有以下三种方式:
- 指定传入类型 — 直接显示对象的数据类型 (最常用)
- 参数模板化 — 将对象中的参数变为模板进行传递
- 整个类模板化 — 将这个对象类型模板化进行传递
#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 << endl;
cout << "年龄:" << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
// 1. 指定传入类型
void printPerson1(Person <string,int> &p)
{
p.showPerson();
}
void test01()
{
Person <string,int> p ("孙悟空",999);
printPerson1(p);
}
// 2. 参数模板化
template<class T1,class T2>
void printPerson2(Person <T1,T2> &p)
//编译器不知道T1和T2的类型,所以需要加关键字template
{
p.showPerson();
cout << "T1的类型为:" << typeid(T1).name() << endl;
// 查看编译器推测的数据类型的结构,不同编译器显示的结果会有所不同
cout << "T2的类型为:" << typeid(T2).name() << endl;
}
void test02()
{
Person <string,int> p ("猪八戒",888);
printPerson2(p);
}
// 3. 整个类模板化
template<class T>
void printPerson3(T &p)
{
p.showPerson();
cout << "T的类型为:" << typeid(T).name() << endl;
}
void test03()
{
Person <string,int> p ("唐三藏",777);
printPerson3(p);
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
1.3.5 类模板与继承
当子类继承的父类是一个类模板时,子类在声明的时候,需指定出父类中T
的类型,如果不指定,编译器无法给子类分配内存。
如果想灵活指定出父类中T的类型,子类也需要变为类模板。
// 类模板与继承
#include<iostream>
using namespace std;
template<class T>
class Base
{
T m;
};
// 指定父类中T的类型
//class Son:public Base // 错误的继承,必须要知道父类的数据类型
class Son:public Base<int> // 正确的继承
{
};
// 子类变为类模板
template<class T1,class T2>
class Son2:public Base<T2>
{
public:
T1 obj;
};
void test02()
{
Son2<int ,char> s2;
}
int main()
{
test02();
system("pause");
return 0;
}
如果直接继承父类,子类并不是模板,数据类型也是未知的,就无法提供内存空间。(不知道大小)。
直接指定父类中数据类型的方法,模板的意义就丧失了,所以就有子类变为类模板的方法。就是把子类和父类中的通用类型都做一个说明。
在子类变为类模板的实现方法中,创建对象的时候,依次给定了T1和T2的数据类型分别为int 和char。这样就知道父类中的数据类型是T2,即char,自己本身的数据类型T1是int类型。
1.3.6 类模板成员函数类外实现
类外实现的时候需要添加以下三点内容:
- 作用域
- 为了和普通的类外实现做区分,还要在作用域后面用
< >
添加模板参数列表(虚拟参数列表) - 为了告诉编译器模板参数列表是什么,还要在前面用关键字template做声明
即使类内函数没有用到参数,类外实现的时候还是要添加以上三点内容。
#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;
// cout << "年龄:" << this->m_Name << 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;
cout << "年龄:" << this->m_Name << endl;
}
void test01()
{
Person<string,int> p("Tom",20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
1.3.7 类模板分文件编写
类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到。
有以下两种解决方法:
- 直接包含.cpp源文件
- 将声明和实现写到同一个文件中,并更改后缀为
.hpp
,.hpp
是约定的名称,并不是强制的。
正常分文件编写的代码如下:
- 头文件
person.h
只写声明
#pragma once
#include<iostream>
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age);
void showPerson();
T1 m_Name;
T2 m_Age;
};
person.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>::showPerson()
{
cout << "姓名:" << this->m_Name;
cout << "年龄:" << this->m_Name << endl;
}
- 测试文件/主函数
test.cpp
// #include"person.h"
#include"person.cpp"
void test01()
{
Person<string, int> p("Tom", 20);
p.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
如果在test.cpp
中包含的是 person.h
,那么编译器只能看到person.h
的内容,而看不到person.cpp
的内容,因为类模板的成员函数是在调用的时候才创建的。
所以第一种解决方法是直接包含person.cpp
文件,因为perosn.cpp
中又包含了person.h
文件,所以编译器可以看到全部内容,但是这种方法不常用。
第二种方法是,将perosn.cpp
中内容写到person.h
中,并person.h
改名为person.hpp
,然后再包含perosn.hpp
文件。这种方法较为常用。
1.3.8 类模板与友元
全局函数类内实现 – 直接在类内声明友元即可
全局函数类外实现 – 需要提前让编译器知道全局函数的存在
#include<iostream>
using namespace std;
#include<string>
template<class T1,class T2>
class Person;
// 类外实现
template<class T1,class T2>
void printPerson2(Person<T1,T2> p)
{
cout << "类外实现" << endl << "姓名:" << p.m_Name << " ";
cout << "年龄:" << p.m_Age << endl;
}
template<class T1,class T2>
class Person
{
// 全局函数 类内实现
friend void printPerson1(Person<T1,T2> p)
{
cout << "类内实现" << endl << "姓名:" << p.m_Name << " ";
cout << "年龄:" << p.m_Age << endl;
}
// 全局函数 类外实现
// friend void printPerson2(Person<T1,T2> p);
//添加空模板参数列表
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;
};
// 类内实现测试
void test01()
{
Person<string, int> p ("张三",19);
printPerson1(p);
}
// 类外实现测试
void test02()
{
Person<string, int> p ("李四",21);
printPerson2(p);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
类模板中,全局函数做友元且类内实现的方法很简单,使用多。
类外实现的方法需要写以下内容:
- 类内声明是普通函数,而类外是现实模板函数。所以需要在类内声明添加空模板列表,使两者一致。
- 还需要使编译器提前知道全局函数的存在,这需要将全局函数写在类之前,并且使用template关键字
- 因为全局函数用到了person类,所以要在全局函数之前声明person类。因为person类中有模板参数,所以也需要关键字tempalte做声明。
1.3.9 类模板案例
实现一个通用的数组类,要求如下:
- 可以对内置数据类型以及自定义数据类型的数据进行存储
- 将数组中的数据存储到堆区
- 构造函数中可以传入数组的容量
- 提供对应的拷贝构造函数以及operator = 防止浅拷贝问题
- 提供尾插法和尾删法对数组中的数据进行增加和删除
- 可以通过下标的方式访问数组中的元素
- 可以获取数组中当前元素个数和数组的容量
代码实现:
#pragma once
#include<iostream>
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 = arr.pAddress;
//深拷贝
this->pAddress = new T[arr.m_Capacity];
for (int i = 0; i < arr.m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
// operator= 防止浅拷贝
MyArray& operator=(const MyArray& arr)
{
//cout << "MyArray重载=的调用" << endl;
// 先判断原来堆区是否有数据,如果有先释放
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];
}
return *this;
}
// 尾插法
void Push_Back(const T& value)
{
if (this->m_Capacity == this->m_Size)
{
cout << "插入失败,数组已满!" << endl;
// 可以用动态数组
return;
}
// 插入
this->pAddress[this->m_Size] = value;
// 更新
this->m_Size++;
}
// 尾删法
void Pop_Back()
{
// 让用户访问不到最后一个元素即为尾删
if (this->m_Size == 0)
{
cout << "删除失败,数组为空!" << endl;
}
this->m_Size--;
}
// 通过下标访问数组中的元素 --- arr[1] = 100
// 重载 [] --- 因为 arr 是自定义类型
T& operator[](int index) // 返回值用引用是为了可以做左值
{
return this->pAddress[index];
}
// 返回数组容量
int getCapacity()
{
return this->m_Capacity;
}
// 返回素组大小
int getSize()
{
return this->m_Size;
}
// 析构函数
~MyArray()
{
if (this->pAddress != NULL)
{
cout << "MyArray析构函数的调用" << endl;
delete[] this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress; // 指针指向堆区开辟的真实数组
int m_Capacity; // 数组容量(总空间)
int m_Size; // 数组大小(当前占据空间)
};
测试代码:
#include"MyArray.hpp"
#include<string>
void printIntArray(MyArray<int>&arr)
{
for (int i = 0; i < arr.getSize(); i++)
cout << arr[i] << " ";
cout << endl;
// 测试了按下标访问
}
void test01()
{
MyArray<int>arr1(5); // 有参构造函数测试
MyArray<int>arr3(100);
arr3 = arr1; // 重载 = 的测试
for (int i = 0; i < 5; i++)
{
// 尾插法插入数据
arr1.Push_Back(i);
}
cout << "arr1的打印输出:" << endl;
printIntArray(arr1);
cout << "arr1的容量为:" << arr1.getCapacity() << endl;
cout << "arr1的大小为:" << arr1.getSize() << endl;
MyArray<int>arr2(arr1); // 拷贝构造函数测试
cout << "arr2的打印输出:" << endl;
printIntArray(arr2);
cout << "arr2的容量为:" << arr2.getCapacity() << endl;
cout << "arr2的大小为:" << arr2.getSize() << endl;
// 尾删
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 test02()
{
MyArray<Person> arr(10);
Person p1("赵", 10);
Person p2("钱", 20);
Person p3("孙", 30);
Person p4("李", 40);
Person p5("周", 50);
// 将数据插入 --- 尾插
arr.Push_Back(p1);
arr.Push_Back(p2);
arr.Push_Back(p3);
arr.Push_Back(p4);
arr.Push_Back(p5);
// 打印数组
printPersonArray(arr);
// 容量
cout << "arr容量:" << arr.getCapacity() << endl;
// 大小
cout << "arr大小:" << arr.getSize() << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
test02的运行结果: