轻松掌握C++的模板与类模板,将Tamplate广泛运用于我们的编程生活

C++提高编程

本阶段主要针对C++泛型编程STL技术做详细讲解,探讨C++更深层的使用

泛型编程:编写与类型无关的通用代码,是代码复用的一种手段。

 

模板

1.模板的概念

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

例如:

 

2.函数模板

C++另一种编程思想称为泛型编程,主要利用的技术就是模板

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

1.函数模板语法

函数模板作用:

建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。

语法

template<typename T>
函数声明或定义

解释

template —— 声明创建模板

typename —— 表面其后面的符号是一种数据类型,可以用class替代

T —— 通过的数据类型,名称可以替换,通常为大写字母,也可以自己定义

示例:

#include<iostream>
using namespace std;
​
//函数案例
​
//两个整型交换的函数
void swapInt(int &a, int &b)//使用C++引用的知识
{
    int temp = a;
    a = b;
    b = temp;
}
​
//两个浮点型交换的函数
void swapDouble(double &a, double &b)//使用C++引用的知识
{
    double temp = a;
    a = b;
    b = temp;
}
​
int main()
{
    int a = 10;
    int b = 20;
    swapInt(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;
    
    return 0;
}

上面的是函数实现数据交换的方式,接下来展示的是函数模板

#include<iostream>
using namespace std;
​
template<typename T>//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是通用的数据类型
void mySwap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}
​
int main()
{
    int a = 10;
    int b = 20;
    
    //利用函数模板交换
    //两种方式使用函数模板
    //1.自动类型推导
    mySwap(a, b);
    
    cout << "a = " << a << " " << "b = " << b << endl;
    
    //2.显示指定类型
    double c = 1.1;
    double d = 2.2;
    mySwap<double>(c, d);
    cout << "c = " << c << " " << "d = " << d << endl;
    
    return 0;
}

总结

  1. 函数模板利用关键字template

  2. 使用函数模板有两种方式:自动类型推导、显示指定类型

  3. 模板的目的是为了提高复用性,将类型参数化

2.函数模板注意事项

注意事项

  1. 自动类型推导,必须推导出一致的数据类型T,才可以使用

  2. 模板必须要确定出T的数据类型,才可以使用

示例:

#include<iostream>
using namespace std;
​
//函数模板注意事项
​
template<typename T>//typename可以替换成class,有的程序员将typename作为函数模板,class作为类模板,作为区分
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, c);//error,推导不出一致的T类型
    cout << "a = " << a << " " << "c = " << c << endl;
}
​
//2.模板必须要确定出T的数据类型,才可以使用
template<class T>
void func()
{
    cout << "func函数的调用" << endl;
}
​
void test02()
{
    func<int>();
}
​
int main()
{
    //test01();
    test02();
    return 0;
}

3.函数模板案例

案例描述:

  1. 利用函数横板封装一个排序的函数,可以对不同数据类型数组进行排序

  2. 排序规则从大到小,排序算法为选择排序

  3. 分别利用char数组int数组进行测试

示例:

#include<iostream>
using namespace std;
​
//实现通用 对数组进行排序的函数
//规则 从大到小
//算法 选择算法
//测试 char 数组 int
​
//交换函数
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;//认定最大值的下标
        for(int j = i;j < len; j++)
        {
            //认定的最大值比遍历出的数值要小,说明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数组
    char charArr[] = "badcfe";
    int num = sizeof(charArr) / sizeof(char);
    mySort(charArr, num);
    printArray(charArr, num);
}
​
void test02()
{
    //测试int数组
    int intArr[] = { 7,5,1,3,9,2,4,6,8 };
    int num = sizeof(intArr) / sizeof(int);
    mySort(intArr, num);
    printArray(intArr, num);
}
​
int main()
{
    test01();
    test02();
    return 0;
}

4.普通函数与函数模板的区别

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

  1. 普通函数调用时可以发生自动类型转换隐式类型转换

  2. 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换

  3. 如果利用显示指定类型的方式,可以发生隐式类型转换

示例:

#include<iostream>
using namespace std;
​
//普通函数调用可以发生隐式类型转换
//函数模板调用,如果利用自动类型推导,不会发生隐式类型转换
//函数模板调用,如果利用显示指定类型的方式,可以发生隐式类型转换
​
//普通函数
int myAdd1(int a, int b)
{
    return a + b;
}
​
void test01()
{
    int a = 10;
    int b = 20;
    char c = 'c';//ASCLL码中a - 97,c - 99
    int ret = myAdd1(a,c);
    cout << ret << endl;//109,会隐式地进行计算
}
​
//函数模板
template<typename T>
T myAdd2(T a, T b)
{
    return a + b;
}
​
void test02()
{
    int a = 1;
    char b = 'b';
    //自动类型推导
    cout << myAdd2(a,b) << endl;//error,出现了两种类型,无法进行运算
    
    //显示指定类型
    cout << myAdd2<int>(a, b) << endl;//99 可以执行运行
}
​
int main()
{
    test01();
    test02();
    return 0;
}

总结:建议使用显示指定类型的方式,调用函数模板,因为可以自己确定通用类型T

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

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数

  2. 可以通过空模板参数列表来强制阀用函数模板

  3. 函数模板也可以发生重载

  4. 如果函数模板可以产生更好的匹配优先调用函数模板

示例

#include<iostream>
using namespace std;
​
//普通函数与函数模板调用规则
//1.如果函数模板和普通函数都可以实现,优先调用普通函数
//2.可以通过空模板参数列表来强制阀用函数模板
//3.函数模板也可以发生重载
//4.如果函数模板可以产生更好的匹配优先调用函数模板
​
void myPrint(int a, int b)
{
    cout << "调用普通函数" << endl;
}
​
template<typename T>
void myPrint(T a, T b)
{
    cout << "调用模板" << endl;
}
​
//void myPrint(T a, T b, T c)
//{
    //cout << "调用模板" << endl;
//}
​
void test01()
{
    int a = 10;
    int b = 10;
    int c = 10;
    myPrint(a, b);//优先调用普通函数,有声名
    
    //通过空模板参数列表,强制调用函数模板
    myPrint<>(a, b);
    
    //函数模板也可以发生重载
    //myPrint(a, b, c);
    
    //如果函数模板产生更好的匹配,优先调用模板函数
    char c1 = 'a';
    char c2 = 'b';
    
    myPrint(c1, c2);
}
​
int main()
{
    test01();
    return 0;
}

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

6.模板的局限性

局限性

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

例如

template<typename 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++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

示例

#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<typename 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 = 10;
    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()
{
    test01();
    test02();
    return 0;
}

总结

  1. 利用具体化的模板,可以解决自定义类型的通用化

  2. 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

类模板

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 << " " << "age:" << this->m_Age << endl;
    }
        
    NameType m_Name;
    AgeType m_Age;
};
​
void test01()
{
    Person<string,int> p1("zhangsan",18);
    p1.showPerson();
}
​
int main()
{
    test01();
    return 0;
}

2.类模板与函数模板区别

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

  1. 类模板没有自动类型推导的使用方式

  2. 类模板在模板参数列表中可以有默认参数

示例

#include<iostream>
#include<string>
using namespace std;
​
//类模板与函数模板区别
template<class NameType, class 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 << " " << "age:" << this->m_Age << endl;
    }
    
    NameType m_Name;
    AgeType m_Age;
};
​
//1.类模板没有自动类型推导使用方式
void test01()
{
    //Person p("zhangsan",18);error,无法使用自动类型推导
    Person<string,int>p("zhangsan",18);//只能使用显示指定函数
    
    p.showPerson();
}
​
//2.类模板在模板参数列表中可以有默认参数
void test02()
{
    Person<string,int>p("lisi",2);
    p.showPerson();
}
​
int main()
{
    test01();
    test02();
    return 0;
}

3.类模板

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  1. 普通类中的成员函数开始就可以创建

  2. 类模板中的成员函数在调用时才创建

示例

#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> m1;//要确定调用的成员类型
    m1.func1();
    
    MyClass<Person2> m2;
    m2.func2();
}
​
int main()
{
    test01();
    return 0;
}

总结:类模板中的成员函数并不是开始就创建的,在调用时才去创建

4.类模板对象做函数参数

学习目标:

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

一共有三种传入方式:

  1. 指定传入的类型——直接显示对象的数据类型

  2. 参数模板化——将对象中的参数变为模板进行传递

  3. 整个类模板化——将这个对象类型模板化进行传递

示例

#include<iostream>
#include<string>
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;
};
​
//1.指定传入形式
void printPerson1(Person<string,int>&p)
{
    p.showPerson();
}
​
void test01()
{
    Person<string,int> p("zhangsan",18);
    
    //1.指定传入形式
    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("lisi",20);
    printPerson2(p);
}
​
//3.整个类模板化
template<class T>
void printPerson(T &p)
{
    p.showPerson();
    cout << "T的数据类型:" << typeid(T).name() << endl;
}
​
void test03()
{
    Person<string,int> p("wangwu",22);
    printPerson(p);
}
​
int main()
{
    test01();
    test02();
    test03();
    return 0;
}
姓名:zhangsan 年龄:18
姓名:lisi 年龄:20
T1的类型:NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
T2的类型:i
姓名:wangwu 年龄:22
T的数据类型:6PersonINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiE

总结

  1. 通过类模板的对象,可以有三种方式向函数中进行传参

  2. 使用比较广泛的是第一种指定传入的类型

typeid的知识补充

在c++中,typeid用于返回指针或引用所指对象的实际类型。

typeid就是C++中查看数据类型的函数,语法一般为:typeid(A).name()

由第二的T1返回值知string的原名是很长的。

示例

#include<iostream> 
using namespace std;
​
class  Base {};
class  Derived
{
public:
};
​
int  main()
{
    Base b, *pb;
    pb = NULL;
    Derived d;
​
    cout  <<  typeid(int).name()  <<  endl
          <<  typeid(unsigned).name()  <<  endl
          <<  typeid(long).name()  <<  endl
          <<  typeid(unsigned long).name()  <<  endl
          <<  typeid(char).name()  <<  endl
          <<  typeid(unsigned char).name()  <<  endl
          <<  typeid(float).name()  <<  endl
          <<  typeid(double).name()  <<  endl
          <<  typeid(string).name()  <<  endl
          <<  typeid(Base).name()  <<  endl
          <<  typeid(b).name()<<endl
          <<  typeid(pb).name()<<endl
          <<  typeid(Derived).name() << endl
          <<  typeid(d).name()<<endl 
          <<  typeid(type_info).name()  <<  endl;
         
     return   0 ;
}

vscode上面输出类型就是如下可见:

i
j
l
m
c
h
f
d
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
4Base
4Base
P4Base
7Derived
7Derived
St9type_info

5.类模板与继承

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

  1. 当子类继承的类是一个类模板时,子类在声明的时候,要指定出父类中T的类型

  2. 如果不指定,编译无法给子类分配内存

  3. 如果想灵活指定出父类中T的类型,子类也需变为类模板

示例

#include<iostream>
#include<string>
using namespace std;
​
//类模板与继承
template<class T>
class Base
{
    T m;
};
​
class S1 :public Base<int>//必须要知道父类中的T类型,才能继承给子类
{
​
};
​
void test01()
{
    S1 s1;
}
​
//想灵活指定父类中T类型,子类也需要变类模板
template<class T1, class T2>
class S2 :public Base<T2>
{
public:
    S()
    {
        cout << "T1类型:" << typeid(T1).name() << endl;
        cout << "T2类型:" << typeid(T2).name() << endl;
    }
    T1 obj;
};
​
void test02()
{
    S2<int,char> s2;
    s2.S();
}
​
int main()
{
    test01();
    test02();
    return 0;
}

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

6.类模板成员函数类外实现

学习目标:够掌控类模板中的成员函数类外实现

示例

#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;
};
​
//构造函数类外实现
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;
}

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

7.类模板分文件编写

学习目标:

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

问题:

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

解决:

解决方法一:直接包含.cpp文件

解决方法二:将声明和实现写到同一文件中,并更改后缀为.hpphpp是约定的名称,并不是强制

示例:

#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;
}
​
void test01()
{
    Person<string,int>p("zhangsan",18);
    p.showPerson();
}
​
int main()
{
    test01();
    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;
}

main.cpp

#include<iostream>
using namespace std;
//第一种方式
#include"person.cpp"//person.h调用的话是不会创建里面的东西
​
//第二种方式,将.h和.cpp中的内容写到一起,将后缀名改为.hpp
​
​
void test01()
{
    Person<string,int>p("zhangsan",18);
    p.showPerson();
}
​
int main()
{
    test01();
    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;
}

main.cpp

#include<iostream>
using namespace std;
//第一种方式
#include"person.hpp"//person.h调用的话是不会创建里面的东西
​
//第二种方式,将.h和.cpp中的内容写到一起,将后缀名改为.hpp
​
​
void test01()
{
    Person<string,int>p("zhangsan",18);
    p.showPerson();
}
​
int main()
{
    test01();
    return 0;
}

8.类模板与友元

学习目标:掌握类模板配合友元函数的类内和类外实现

全局函数类内实现——直接在类内声明友元即可

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

//提前让编译器知道有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;
}

示例

#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;
    }
    
private:
    T1 m_Name;
    T2 m_Age;
};
​
void test01()
{
    Person<string,int>p("Tom",20);
    printPerson(p);
}
​
void test02()
{
    Person<string,int>p("Jerry",18);
    printPerson2(p);
}
​
int main()
{
    test01();
    test02();
    return 0;
}

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

类模板案例

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

  1. 可以对内置数据类型以及自定义数据类型的数据进行存储

  2. 将数组中的数据存储到堆区

  3. 构造函数中可以传入数组的容量

  4. 提供对应的拷贝构造函数以及operator=防止浅拷贝问题

  5. 提供尾插法和尾删除法对数组中的数据进行增加和删除

  6. 可以通过下标的方式访问数组中的元素

  7. 可以获取数组中当前元素个数和数组的容量

函数实现

MyArray.hpp

#pragma
#include<iostream>
#include<string>
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= 防止浅拷贝问题 a = b = c
    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)
        {
            return;
        }
        this->pAddress[this->m_Size] = val;//在数组末尾插入数据
        this->m_Size++;//更新数组大小
    }
    
    //尾删法
    void Pop_Back()
    {
        //让用户访问不到最后一个元素,即为尾删,逻辑删除
        if(this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;//更新数组大小
    }
    
    //通过下标方式访问数组中的元素 arr[0] = 100 赋值操作
    T& operator[](int index)
    {
        return this->pAddress[index];
    }
    
    //返回数组容量
    int getCapacity()
    {
        return this->m_Capacity;
    }
    
    //返回数组大小
    int getSize()
    {
        return this->m_Size;
    }
    
    //析构函数
    ~MyArray()
    {
        //cout << "MyArray析构函数的调用" << endl;
        if(this->pAddress != NULL)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }
​
private:
    T * pAddress;//指针指向堆区开辟的真实数组
    
    int m_Capacity;//数组容量
    
    int m_Size;//数组大小
};

main.cpp

#include<iostream>
#include<string>
using namespace std;
#include"MyArray.hpp"
​
void test01()
{
    //测试构造函数、析构函数、operator的调用,使用完课注释掉
    MyArray<int>arr1(5);
    MyArray<int>arr2(arr1);
    MyArray<int>arr3(100);
    arr3 = arr1;
}
​
void printIntArray(MyArray<int>&arr)
{
    for(int i = 0;i < arr.getSize(); i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}
​
void test02()
{
    //测试尾插法、尾删法和其余函数调用
    MyArray<int>arr1(5);
    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;
}
​
//测试自定义数据类型
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 test03()
{
    MyArray<Person>arr(10);
    
    Person p1("zhangsan",18);
    Person p2("lisi",19);
    Person p3("wangwu",20);
    Person p4("zhaoliu",21);
    Person p5("tangqi",22);
    
    //将数据插入到数组中
    arr.Push_Back(p1);
    arr.Push_Back(p2);
    arr.Push_Back(p3);
    arr.Push_Back(p4);
    arr.Push_Back(p5);
    
    //打印数组
    printPersonArray(arr);
    
    //打印数组容量
    cout << "数组容量:" << arr.getCapacity() << endl;
    //打印数组大小
    cout << "数组大小:" << arr.getSize() << endl;
}
​
int main()
{
    test01();
    test02();
    test03();
    return 0;
}

        今天我们全面学习的模板和类模板这一块的知识,就到此为止啦。掌握和运用,还需要持续不断的训练和磨合,加深对模板板块的理解,这在未来,对我们的提升自己编码水平和能力,都是非常重要的。通过学长参加招聘会,我了解到今年春招情况惨淡,大部分公司都对软件开发部门进行了比较具有规模性的裁员。应对这样残酷的现实,我们只能不断提高自己,踏实掌握技术才是硬道理!

码字不易,希望大家多多支持!!

猜你喜欢

转载自blog.csdn.net/Williamtym/article/details/129434968