Wiki
Template
Existing English translations were reused. 4 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.Template
1.1.Concept of Templates
A template is to establisha general-purpose mold,greatlyimprove reusability
For example, templates in daily life
One-inch photo template:

PPT template:


Features of the template:
- A template cannot be used directly; it is only a framework.
- The generality of a template is not omnipotent.
1.2.Function template
-
C++ Another programming paradigm is called ==generic programming==, and the main technique it uses is templates.
-
C++ Two template mechanisms are provided:function templatesandclass templates
1.2.1.Function template syntax
Function template purpose:
Create a generic function whose return type and parameter types may be left unspecified, represented by avirtual type.
Syntax:
template<typename T>
函数声明或定义
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
Explanation:
template --- Declare creation template
typename --- The symbol following it indicates a data type; it can be replaced by class.
T --- Generic data type; the name can be replaced, usually an uppercase letter
Example:
//交换整型函数
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>
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
void test01()
{
int a = 10;
int b = 20;
//swapInt(a, b);
//利用模板实现交换
//1、自动类型推导
mySwap(a, b);
//2、显示指定类型
mySwap<int>(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main() {
test01();
return 0;
}
Operation result:
a = 10
b = 20
Summary:
- Function templates use the keyword template
- There are two ways to use function templates: automatic type deduction and explicit type specification
- The purpose of templates is to improve reusability by parameterizing types
1.2.2.Function Template Considerations
Note:
-
For automatic type deduction, a consistent data type T must be deduced before it can be used.
-
The template must determine the data type of T before it can be used.
Example:
//利用模板提供通用的交换函数
template<class T>
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
// 1、自动类型推导,必须推导出一致的数据类型T,才可以使用
void test01()
{
int a = 10;
int b = 20;
char c = 'c';
mySwap(a, b); // 正确,可以推导出一致的T
//mySwap(a, c); // 错误,推导不出一致的T类型
}
// 2、模板必须要确定出T的数据类型,才可以使用
template<class T>
void func()
{
cout << "func 调用" << endl;
}
void test02()
{
//func(); //错误,模板不能独立使用,必须确定出T的类型
func<int>(); //利用显示指定类型的方式,给T一个类型,才可以使用该模板
}
int main() {
test01();
test02();
return 0;
}
Operation result:
func 调用
Summary:
- When using templates, the generic data type T must be determined, and consistent types must be able to be deduced.
1.2.3.Function template case
Case description:
- 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
- 排序规则从大到小,排序算法为选择排序
- 分别利用char数组和int数组进行测试
Example:
//交换的函数模板
template<typename T>
void mySwap(T &a, T&b)
{
T temp = a;
a = b;
b = temp;
}
template<class T> // 也可以替换成typename
//利用选择排序,进行对数组从大到小的排序
void mySort(T arr[], int len)
{
for (int i = 0; i < len; i++)
{
int max = i; //最大数的下标
for (int j = i + 1; j < len; j++)
{
if (arr[max] < arr[j])
{
max = j;
}
}
if (max != i) //如果最大数的下标不是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数组
char charArr[] = "bdcfeagh";
int num = sizeof(charArr) / sizeof(char);
mySort(charArr, num);
printArray(charArr, num);
}
void test02()
{
//测试int数组
int intArr[] = { 7, 5, 8, 1, 3, 9, 2, 4, 6 };
int num = sizeof(intArr) / sizeof(int);
mySort(intArr, num);
printArray(intArr, num);
}
int main() {
test01();
test02();
return 0;
}
Run result (type size or type name may vary depending on compiler and platform):
h g f e d c b a \x00
9 8 7 6 5 4 3 2 1
Summary: Templates can improve code reuse and need to be mastered proficiently.
1.2.4.Difference between ordinary functions and function templates
Difference between ordinary functions and function templates:
- Automatic type conversion (implicit type conversion) can occur during ordinary function calls
- When a function template is called, if automatic type deduction is used, implicit type conversion will not occur
- If the type is explicitly specified, implicit type conversion can occur
Example:
//普通函数
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); //正确,如果用显示指定类型,可以发生隐式类型转换
}
int main() {
test01();
return 0;
}
Operation result:
109
Summary: It is recommended to call function templates by explicitly specifying the type, because you can determine the generic type T yourself.
1.2.5.Calling rules for ordinary functions and function templates
The calling rules are as follows:
- If both a function template and a normal function can implement the call, the normal function is preferred
- You can force a call to the function template by using an empty template argument list
- Function templates can also be overloaded
- If the function template can produce a better match, the function template is preferred
Example:
//普通函数与函数模板调用规则
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();
return 0;
}
Operation result:
调用的普通函数
调用的模板
调用重载的模板
调用的模板
Summary: Now that a function template is provided, it is best not to provide an ordinary function; otherwise, ambiguity is likely to occur.
1.2.6.Limitations of Templates
Limitations:
- The generality of a template is not omnipotent.
For example:
template<class T>
void f(T a, T b)
{
a = b;
}
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
With the assignment operation provided in the above code, if the passed-in a and b are arrays, it cannot be implemented.
For another example:
template<class T>
void f(T a, T b)
{
if(a > b) { ... }
}
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
In the above code, if the data type passed for T is a custom data type like Person, it also cannot run properly
Therefore, C++ to solve this problem, overloads of the template are provided, so that for thesespecific typesprovidespecialized templates
Example:
#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;
}
}
//具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型
//具体化优先于常规模板
template<> bool myCompare(Person &p1, Person &p2)
{
if ( p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age)
{
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();
return 0;
}
Operation result:
a != b
p1 == p2
Summary:
- By using instantiated templates, generalization of custom types can be achieved.
- Learning templates is not for the purpose of writing templates, but rather to be able to use the templates provided by the system in the STL.
1.3.Class template
1.3.1.Class template syntax
Purpose of class templates:
- Create a generic class in which the data type of its members does not need to be specified concretely, and use avirtual typeto represent it.
Syntax:
template<typename T>
类
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
Explanation:
template --- Declare creation template
typename --- The symbol following it indicates a data type; it can be replaced by class.
T --- Generic data type; the name can be replaced, usually an uppercase letter
Example:
#include <string>
//类模板
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("孙悟空", 999);
P1.showPerson();
}
int main() {
test01();
return 0;
}
Operation result:
name: 孙悟空 age: 999
Summary: Class templates and function templates have similar syntax; after declaring the template template, add a class, and this class is called a class template.
1.3.2.Difference between class templates and function templates
There are mainly two differences between class templates and function templates:
- Class templates have no usage form with automatic type deduction.
- Class templates can have default parameters in the template parameter list.
Example:
#include <string>
//类模板
template<class NameType, class AgeType = int>
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;
};
//1、类模板没有自动类型推导的使用方式
void test01()
{
// Person p("孙悟空", 1000); // 错误 类模板使用时候,不可以用自动类型推导
Person <string ,int>p("孙悟空", 1000); //必须使用显示指定类型的方式,使用类模板
p.showPerson();
}
//2、类模板在模板参数列表中可以有默认参数
void test02()
{
Person <string> p("猪八戒", 999); //类模板中的模板参数列表 可以指定默认参数
p.showPerson();
}
int main() {
test01();
test02();
return 0;
}
Operation result:
name: 孙悟空 age: 1000
name: 猪八戒 age: 999
Summary:
- Class templates can only be used by explicitly specifying the type.
- The template parameter list in a class template may have default parameters.
1.3.3.Timing of member function instantiation in class templates
There is a difference in the timing of creation between member functions in class templates and member functions in ordinary classes:
- Member functions in an ordinary class can be created from the start.
- Member functions in a class template are created only when they are called.
Example:
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 fun1() { obj.showPerson1(); }
void fun2() { obj.showPerson2(); }
};
void test01()
{
MyClass<Person1> m;
m.fun1();
//m.fun2();//编译会出错,说明函数调用才会去创建成员函数
}
int main() {
test01();
return 0;
}
Operation result:
Person1 show
Summary: Member functions in class templates are not created at the beginning; they are created only when called.
1.3.4.Class template objects as function parameters
Learning objectives:
- Ways of passing objects instantiated from class templates as function arguments
There are three input methods in total:
- Specify the passed-in type --- directly display the data type of the object
- Parameter templating --- turn the parameters in the object into templates for passing
- Template the entire class --- template this object type for passing
Example:
#include <string>
//类模板
template<class NameType, class AgeType = int>
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;
};
//1、指定传入的类型
void printPerson1(Person<string, int> &p)
{
p.showPerson();
}
void test01()
{
Person <string, int >p("孙悟空", 100);
printPerson1(p);
}
//2、参数模板化
template <class T1, class T2>
void printPerson2(Person<T1, T2>&p)
{
p.showPerson();
cout << "T1的类型为: " << typeid(T1).name() << endl;
cout << "T2的类型为: " << typeid(T2).name() << endl;
}
void test02()
{
Person <string, int >p("猪八戒", 90);
printPerson2(p);
}
//3、整个类模板化
template<class T>
void printPerson3(T & p)
{
cout << "T的类型为: " << typeid(T).name() << endl;
p.showPerson();
}
void test03()
{
Person <string, int >p("唐僧", 30);
printPerson3(p);
}
int main() {
test01();
test02();
test03();
return 0;
}
Run result (type size or type name may vary depending on compiler and platform):
name: 孙悟空 age: 100
name: 猪八戒 age: 90
T1的类型为: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
T2的类型为: i
T的类型为: 6PersonINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiE
name: 唐僧 age: 30
Summary:
- An object created via a class template can be passed as an argument to a function in three ways.
- The most widely used is the first one: specifying the type passed in.
1.3.5.Class Templates and Inheritance
When a class template encounters inheritance, the following points need to be noted:
- When the parent class inherited by a subclass is a class template, the subclass must specify the type of T in the parent class when it is declared.
- If not specified, the compiler cannot allocate memory for the subclass.
- If you want to flexibly specify the type of T in the parent class, the subclass must also become a class template.
Example:
template<class T>
class Base
{
T m;
};
//class Son:public Base //错误,c++编译需要给子类分配内存,必须知道父类中T的类型才可以向下继承
class Son :public Base<int> //必须指定一个类型
{
};
void test01()
{
Son c;
}
//类模板继承类模板 ,可以用T2指定父类中的T类型
template<class T1, class T2>
class Son2 :public Base<T2>
{
public:
Son2()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
}
};
void test02()
{
Son2<int, char> child1;
}
int main() {
test01();
test02();
return 0;
}
Run result (type size or type name may vary depending on compiler and platform):
i
c
Summary: If the parent class is a class template, the child class must specify the data type of T in the parent class
1.3.6.Out-of-class implementation of class template member functions
Learning objective: Be able to master the out-of-class implementation of member functions in class templates
Example:
#include <string>
//类模板中成员函数类外实现
template<class T1, class T2>
class Person {
public:
//成员函数类内声明
Person(T1 name, T2 age);
void showPerson();
public:
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;
}
void test01()
{
Person<string, int> p("Tom", 20);
p.showPerson();
}
int main() {
test01();
return 0;
}
Operation result:
姓名: Tom 年龄:20
Summary: When implementing member functions of a class template outside the class, the template parameter list must be added.
1.3.7.Writing class templates in separate files
Learning objectives:
- Master the problems arising from writing class template member functions across multiple files and their solutions.
Question:
- The member functions in a class template are instantiated at the call stage, which causes linking failures when writing code in separate files.
Solution:
- Solution 1: Directly include the .cpp source file
- Solution 2: Write the declaration and implementation into the same file, and change the extension to .hpp. hpp is a conventional name, not a mandatory one
Example:
person.hpp中代码:
#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;
};
//构造函数 类外实现
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;
}
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
类模板分文件编写.cpp中代码
#include<iostream>
using namespace std;
//#include "person.h"
#include "person.cpp" //解决方式1,包含cpp源文件
//解决方式2,将声明和实现写到一起,文件后缀名改为.hpp
#include "person.hpp"
void test01()
{
Person<string, int> p("Tom", 10);
p.showPerson();
}
int main() {
test01();
return 0;
}
Operation result: This code block depends on the person.cpp in the same example and cannot be compiled independently; after building together with the preceding class template implementation file, this section itself has no terminal output.
Summary: The mainstream solution is the second one, which is to write the class template member functions together and change the file extension to .hpp
1.3.8.Class Templates and Friends
Learning objectives:
- Master the in-class and out-of-class implementations of class templates with friend functions
Global function implementation within a class - simply declare it as a friend inside the class
Implementation of global functions outside the class - the compiler needs to be made aware of the existence of the global function in advance
Example:
#include <string>
//2、全局函数配合友元 类外实现 - 先做函数模板声明,下方在做函数模板定义,在做友元
template<class T1, class T2> class Person;
//如果声明了函数模板,可以将实现写到后面,否则需要将实现体写到类的前面让编译器提前看到
//template<class T1, class T2> void printPerson2(Person<T1, T2> & p);
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 printPerson(Person<T1, T2> & p)
{
cout << "姓名: " << p.m_Name << " 年龄:" << p.m_Age << endl;
}
//全局函数配合友元 类外实现
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;
};
//1、全局函数在类内实现
void test01()
{
Person <string, int >p("Tom", 20);
printPerson(p);
}
//2、全局函数在类外实现
void test02()
{
Person <string, int >p("Jerry", 30);
printPerson2(p);
}
int main() {
//test01();
test02();
return 0;
}
Operation result:
类外实现 ---- 姓名: Jerry 年龄:30
Summary: It is recommended that global functions be implemented inside the class; usage is simple, and the compiler can recognize them directly.
1.3.9.Class Template Case
Case description: Implement a generic array class with the following requirements:
- Can store data of built-in data types as well as custom data types
- Store the data in the array into the heap area
- The capacity of the array can be passed in through the constructor
- Provide the corresponding copy constructor and operator= to prevent shallow copy issues
- Provide tail insertion and tail deletion methods to add and delete data in the array
- Elements in the array can be accessed by subscript
- The current number of elements in the array and the capacity of the array can be obtained
Example:
myArray.hpp code in
#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyArray
{
public:
//构造函数
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
pAddress = new T[this->m_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++)
{
//如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,
// 普通类型可以直接= 但是指针类型需要深拷贝
this->pAddress[i] = arr.pAddress[i];
}
}
//重载= 操作符 防止浅拷贝问题
MyArray& operator=(const MyArray& myarray) {
if (this->pAddress != NULL) {
delete[] this->pAddress;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = myarray.m_Capacity;
this->m_Size = myarray.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++) {
this->pAddress[i] = myarray[i];
}
return *this;
}
//重载[] 操作符 arr[0]
T& operator [](int index)
{
return this->pAddress[index]; //不考虑越界,用户自己去处理
}
//尾插法
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--;
}
//获取数组容量
int getCapacity()
{
return this->m_Capacity;
}
//获取数组大小
int getSize()
{
return this->m_Size;
}
//析构
~MyArray()
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
}
private:
T * pAddress; //指向一个堆空间,这个空间存储真正的数据
int m_Capacity; //容量
int m_Size; // 大小
};
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
类模板案例—数组类封装.cpp中
#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> array1(10);
for (int i = 0; i < 10; i++)
{
array1.Push_back(i);
}
cout << "array1打印输出:" << endl;
printIntArray(array1);
cout << "array1的大小:" << array1.getSize() << endl;
cout << "array1的容量:" << array1.getCapacity() << endl;
cout << "--------------------------" << endl;
MyArray<int> array2(array1);
array2.Pop_back();
cout << "array2打印输出:" << endl;
printIntArray(array2);
cout << "array2的大小:" << array2.getSize() << endl;
cout << "array2的容量:" << array2.getCapacity() << endl;
}
//测试自定义数据类型
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 test02()
{
//创建数组
MyArray<Person> pArray(10);
Person p1("孙悟空", 30);
Person p2("韩信", 20);
Person p3("妲己", 18);
Person p4("王昭君", 15);
Person p5("赵云", 24);
//插入数据
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;
cout << "pArray的容量:" << pArray.getCapacity() << endl;
}
int main() {
//test01();
test02();
return 0;
}
Run result: This code block depends on the myArray.hpp in the same case and cannot be compiled independently; after completing the header files, the run result is produced by the array printing logic in main.
Summary:
Be able to use the learned knowledge points to implement a general-purpose array