IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【C++自学笔记 提高编程篇(一)模板】 -> 正文阅读

[C++知识库]【C++自学笔记 提高编程篇(一)模板】

本阶段主要针对C++泛型编程STL技术做详细学习,探讨C++更深层的使用
谨记:温故而知新。

1 模板

1.1 模板的概念

模板就是建立通用的模具,大大提高复用性

  • 例如生活中的模板:
    • 一寸照片模板
    • PPT模板
  • 模板的特点:
    • 模板不可以直接使用,它只是一个框架
    • 模板的通用并不是万能的
  • C++另一种编程思想称为泛型编程,主要利用的技术就是模板
  • C++提供两种模板机制:
    • 函数模板
    • 类模板

首先我们来看看函数模板吧~

1.2 函数模板

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);
    //利用函数模板交换
    //两种方式
    //1.自动类型推导
    mySwap(a, b);
    //2.显示指定类型
    mySwap<int>(a, b);
    cout << "a: " << a << "  b: " << b << endl;

    double c = 1.1;
    double d = 2.2;
    swapDouble(c, d);
    cout << "c = " << c << "  d = " << d  << endl;
}
int main(){
    test01();
    system("pause");
    return 0;
}

总结:

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

1.2.2 函数模板注意事项

注意事项:

  • 自动类型推导,必须推导出一致的数据类型T,才可以使用
  • 模板必须要确定出T的数据类型,才可以使用
    Alt

1.2.3 函数模板案例

案例描述

  • 利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序
  • 排序规则从大到小,排序算法为选择排序
  • 分别利用char数组int数组进行测试

代码示例:

#include <iostream>
using namespace std;xzvx//实现通用 对数组进行排序的函数
//规则 从大到小
//算法 选择
//测试 char数组、int数组

//交换函数模板
template<class T>
void mySwap(T &a, T &b){
    T temp = a;
    a = b;
    b = temp;
}
//排序算法
template<class T>
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){
            mySwap(arr[max], arr[i]);
        }
    }
}
//提供打印数组模板
template<class T>
void printArray(T arr[], int len){
    for(int i = 0; i < len; i++){
        cout << arr[i] <<" ";
    }
    cout << endl;
}
void test01(){
    //测试char数组
    char charArr[] = "jklae";
    int num = sizeof(charArr) / sizeof(char);
    mySort(charArr, num);
    printArray(charArr, num);
}
void test02(){
    //测试int数组
    int intArr[] = {3, 5, 2, 9, 6, 7};
    int num = sizeof (intArr) / sizeof (int);
    mySort(intArr, num);
    printArray(intArr, num);
}
int main(){
    test01();
    test02();
    system("pause");
    return 0;
}

运行结果:
在这里插入图片描述

1.2.4 普通函数与函数模板的区别

普通函数与函数模板区别:

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换

为了对两者区别有更清晰的认知,接下来我们来看几个例子。

  • 首先写一个普通函数,返回值为两个数之和。
    代码示例:
#include <iostream>
using namespace std;
//普通函数
int myAdd01(int a, int b){
    return a+b;
}
void test01(){
    int a = 10;
    int b = 20;
    char c = 'c';
    cout << myAdd01(a, c) << endl;
}
int main(){
    test01();
    system("pause");
    return 0;
}
  • 运行结果:
    Alt
  • 由运行结果可知,当调用普通函数时,char类型的c被转换为int型的与之对应的ASSCI码值,再与a相加。
  • 而当直接调用函数模板时,则不会发生隐式类型转换,反而会报错。
    Alt
  • 因此当调用函数模板时,需利用显示指定类型的方式,才可以发生隐式类型转换。
    Alt
  • 总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T。

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

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数
  2. 可以通过空模板参数列表来强制调用函数模板
  3. 函数模板也可以发生重载
  4. 如果函数模板可以产生更好的匹配,优先调用函数模板

示例代码

#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 = 10;
    //1.如果函数模板和普通函数都可以实现,优先调用普通函数
    myPrint(a, b);
    //2.通过空模板参数列表,强制调用函数模板
    myPrint<>(a, b);
    //3. 函数模板也可以发生重载
    myPrint(a, b, 20);
    //4. 如果函数模板可以产生更好的匹配,优先调用函数模板
    char c1 = 'a';
    char c2 = 'b';
    myPrint(c1, c2);
}
int main(){
    test01();
    system("pause");
    return 0;
}

运行结果:
在这里插入图片描述

1.2.6 模板的局限性

局限性

模板的通用性并不是万能的

代码示例1

void f(T a, T b){
    a = b;
}

在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了
代码示例2

template<class T>
void f(T a, T b){
    if(a > b) {...}
}

在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行

因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板(前面所说过的运算符重载也可以实现,不过比较麻烦,此处不考虑)

代码示例

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

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;
    }
}
//利用具体化Person的版本实现代码,具体化优先调用
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);
    bool ret = myCompare(p1, p2);
    if(ret){
        cout << "p1 == p2" << endl;
    }else{
        cout << "p1 != p2" << endl;
    }
}
int main(){
    test02();
    system("pause");
    return 0;
}

总结:

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

1.3 类模板

1.3.1 类模板语法

类模板作用

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

语法
template<typename T>

解释:

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

示例代码

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

//类模板
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 : " << this->m_Name << endl;
        cout << "age : " << this->m_Age << endl;
    }
    NameType m_Name;
    AgeType m_Age;
};
void test01(){
    //指定上面的NameType和AgeType
    Person<string, int>p1("孙悟空", 999);
    p1.showPerson();

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

总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板。

1.3.2 类模板和函数模板区别

类模板与函数模板区别主要有两点:

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

在类模板参数列表中使用默认参数(AgeType的默认参数为int

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 : " << this->m_Name << endl;
        cout << "age : " << this->m_Age << endl;
    }
    NameType m_Name;
    AgeType m_Age;
};
  • 当说明了默认参数时,调用模板时没向有默认参数那一项声明类型时会自定使用声明的默认类型。
void test01(){
    //指定上面的NameType
    Person<string>p1("孙悟空", 999.1);
    p1.showPerson();

}

运行结果:
Alt

  • 若传了,则使用所传类型。
void test01(){
    //指定上面的NameType
    Person<string, double>p1("孙悟空", 999.1);
    p1.showPerson();
}

运行结果:
在这里插入图片描述

总结:

  • 类模板使用只能用显示指定类型方式
  • 类模板中的模板参数列表可以有默认参数

1.3.4 类模板对象做函数参数

学习目标

  • 类模板实例化出的对象,向函数传参的方式

一共有三种传入方式

  1. 指定传入的类型 (最常用) – 直接显示对象的数据类型
void PrintPerson01(Person<string, int>&p){
    p.showPerson();
}
void test01(){
    Person<string, int>p("absdui", 23);
    PrintPerson01(p);
}
  1. 参数模板化 – 将对象中的参数变为模板进行传递
template<class T1, class T2>
void PrintPerson02(Person<T1, T2>&p){
    p.showPerson();
    //通过以下语句可以看出T1,T2的类型分别为string,int
    cout << "T1的类型为:" << typeid(T1).name() << endl;
    cout << "T2的类型为:" << typeid(T2).name() << endl;
}
void test02(){
    Person<string, int>p("absdui", 23);
    PrintPerson02(p);
}
  1. 整个类模板化 – 将这个对象类型模板化进行传递
template<class T>
void PrintPerson03(T &p){
    p.showPerson();
    cout << "T的数据类型为: " << typeid(T).name() << endl;
}
void test03(){
    Person<string, int>p("absdui", 23);
    PrintPerson03(p);
}

总结:

  • 通过类模板创建的对象,可以有三种方式向函数中进行传参
  • 使用比较广泛是第一种:指定传入的类型

1.3.5 类模板与继承

当类模板碰到继承时,需要注意一下几点:

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

接下来看看代码示例:

  1. 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型
//类模板与继承
template<class T>
class Base{
    T m;
};
//class Son :public Base错误,必须要知道父类中T的类型,才能继承给子类
class Son :public Base<int>{
    
};
void test01(){
    Son s1;
}
  1. 如果想灵活指定出父类中T的类型,子类也需变为类模板
template<class T>
class Base{
    T m;
};
template<class T1, class T2>
class Son2 :public Base<T2>{
public:
    Son2(){
        cout << "T1类型:" << typeid(T1).name() << endl;
        cout << "T2类型:" << typeid(T2).name() << endl;
    }
    T1 m1;
};
void test02(){
    Son2<int, char>s2;
}

总结:如果父类是类模板,子类需要指定出父类中T的数据类型

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

代码示例

#include <iostream>
using namespace std;
//类模板成员函数类外实现
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;
}
void test01(){
    Person<string, int>p("tom", 20);
    p.showPerson();
}
int main(){
    test01();
    system("pause");
    return 0;
}

总结:类模板中成员函数类外实现时,需要加上模板参数列表。

1.3.7 类模板分文件编写

学习目标

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

问题

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

解决

  • 解决方式1:直接包含.cpp源文件

main.cpp:

#include <iostream>
#include "person.cpp"
//#include <string>
using namespace std;

void test01(){
    Person<string, int>p("Jerry", 18);
    p.showPerson();
}
int main(){
    test01();
    system("pause");
    return 0;
}

person.h:

#pragma once
#include <iostream>
#include <string>
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 << " 年龄:" << this->m_Age << endl;
}
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpphpp是约定的名称,并不是强制

main.cpp:

#include <iostream>
#include "person.hpp"
//#include <string>
using namespace std;

void test01(){
    Person<string, int>p("Jerry", 18);
    p.showPerson();
}
int main(){
    test01();
    system("pause");
    return 0;
}

person.hpp:

#pragma once
#include <iostream>
#include <string>
using namespace std;
//类模板分文件编写问题以及解决
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;
}

总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp

1.3.8 类模板与友元

  • 学习目标:
    • 掌握类模板配合友元函数的类内和类外实现
  • 全局函数类内实现:直接在类内声明友元即可
#include <iostream>
#include <string>
using namespace std;

template<class T1, class T2>
class Person{
    //全局函数类内实现
    friend void printPerson(Person<T1, T2>p){
        cout << " 姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
    }
public:
    Person(T1 name, T2 age){
        this->m_Name = name;
        this->m_Age = age;
    }
    T1 m_Name;
    T2 m_Age;
};
//1.全局函数类内实现
void test01(){
    Person<string, int>p("Tom", 20);
    printPerson(p);
}
int main(){
    test01();
    system("pause");
    return 0;
}
  • 全局函数类外实现:需要提前让编译器知道全局函数的存在
#include <iostream>
#include <string>
using namespace std;

//通过全局函数 打印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{
    //全局函数类内实现
    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;
    }
    T1 m_Name;
    T2 m_Age;
};

//2.全局函数类外实现
void test02(){
    Person<string, int>p("Tom", 20);
    printPerson2(p);
}
int main(){
    test02();
    system("pause");
    return 0;
}

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

1.3.9 类模板案例

案例描述:实现一个通用的数组类,要求(7个)如下:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储(运用本篇普通函数与函数模板调用规则的内容/1.2.5
  • 将数组中的数据存储到堆区
  • 构造函数中可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题(核心编程篇:运算符重载/4.5.4
  • 提供尾插法和尾删法对数组中的数据进行增加和删除(提供一些接口)
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组的容量 (容量私有化,只提供对外接口)

思路图
Alt

先声明数组类和各种构造、析构函数,保证基本的代码运行正常。

MyArray.hpp:

//自己通用的数组类
#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];
        //将arr中数据都拷贝过来
        for(int i = 0; i < this->m_Size; i++){
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    //operator= 防止浅拷贝问题
    MyArray& operator=(const MyArray& arr){
        cout << "MyArray的operator=调用" << 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 < this->m_Size; i++){
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
    //析构函数
    ~MyArray(){
        if(this->pAddress != NULL){
            cout << "MyArray的析构函数调用" << endl;
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
private:
    T * pAddress;  //指针指向堆区开辟的真实数组
    int m_Capacity; //数组容量
    int m_Size;  //数组大小
};

main.cpp:

#include <iostream>
#include "MyArray.hpp"
void test01(){
    MyArray<int>arr1(5);  //有参构造测试 析构测试
    MyArray<int>arr2(arr1); //拷贝构造测试
    //operator=测试
    MyArray<int>arr3(100);
    arr3 = arr1;
}
int main() {
    test01();
    system("pause");
    return 0;
}

测试结果(成功):
Alt

最后实现其余具体功能,并进行测试(增删、通过下标访问、获取当前大小与容量):

MyArray.hpp:

//自己通用的数组类
#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];
        //将arr中数据都拷贝过来
        for(int i = 0; i < this->m_Size; i++){
            this->pAddress[i] = arr.pAddress[i];
        }
    }
    //operator= 防止浅拷贝问题
    MyArray& operator=(const MyArray& arr){
        //cout << "MyArray的operator=调用" << 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 < 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){
            cout << "Sorry, the arr was full!" << endl;
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++; //更新数组大小
    }
    //尾删法
    void Pop_Back(){
        //让用户访问不到最后一个元素,即为尾删,逻辑删除
        if(this->m_Size == 0){
            cout << "the size was zero!" << endl;
            return;
        }
        this->m_Size--;
    }
    //通过下标方式访问数组元素  并且可以作为左值 则需返回引用 将本身作为返回
    //重载[] , []返回值为T
    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;  //数组大小
};

main.cpp:

#include <iostream>
#include <string>
#include "MyArray.hpp"

void printArray(MyArray<int>& arr){
    for(int i = 0; i < arr.getSize(); i++){
        cout << arr[i] << endl;
    }
}
void test01(){
    MyArray<int>arr1(5);
    for(int i = 0; i < 5; i++){
        arr1.Push_Back(i);
    }
    cout << "arr1的打印输出为:" << endl;
    printArray(arr1);
    cout << "arr1的容量为:" << arr1.getCapacity() << endl;
    cout << "arr1的大小为:" << arr1.getSize() << endl;

    MyArray<int>arr2(arr1);
    cout << "arr2的打印输出为:" << endl;
    //尾删测试
    arr2.Pop_Back();
    cout << "arr2尾删后" << endl;
    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("mary", 23);
    Person p2("fire", 34);
    Person p3("lus", 67);
    Person p4("miss", 45);
    Person p5("li", 23);
    //将数据插入到数组中
    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() {
    //test01();
    test02();
    system("pause");
    return 0;
}

测试运行结果:
Alt

至此,模板学习告一段落。
能够将前面所学的运算符重载、函数重载、深浅拷贝等等知识和当前知识(各种模板的定义声明与调用、以及各种情况下的调用规则)合理运用,综合掌握,这是非常重要的一件事。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 11:35:34  更:2022-10-31 11:39:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 4:48:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码