类模版创建数组

2021-08-04  本文已影响0人  Sheik

环境:ide:Mac+clion

视频链接:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5

需求:通过模版类,实现一个数组。

#include <iostream>

using namespace std;
#ifndef CHAPTER6_MYARRAY_HPP
#define CHAPTER6_MYARRAY_HPP


template<class T>
class MyArray {

public:
    //构造函数
    MyArray(int len) {
        cout << "MyArray 构造函数调用" << endl;
        this->m_Capacity = len;//这里设定容量大小,构造函数传递进来。
        this->address = new T[len];
        m_Size = 0;
    }

    //尾插法
    void push_back(const T &data) {
        if (this->m_Capacity > this->m_Size) {
            this->address[this->m_Size] = data;//这个是再数组后面插入数据。
            this->m_Size++;//数组大小进行更新。
        } else {
            cout << "已经超过容量,无法再插入。"<<endl;
            return;
        }
    }

    //尾删法
    void pull_back(){
        if (this->m_Size == 0){
            cout << "数组为空,无法删除数据。"<<endl;
            return;
        }
//        this->address[this->m_Size] = NULL;//删除最后一个数据,可以不进行设置为空,通过下面代码进行违删除
        this->m_Size--;//更新数组的大小。
    }

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

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

    //通过下标方式访问。
    T & operator[](int index){
        return this->address[index];
    }

    //有了构造就需要析构函数
    ~MyArray() {
        if (this->address != NULL) {
            cout << "MyArray 析构函数调用" << endl;
            delete[]this->address;
            this->address = NULL;
        }
    }

    //拷贝构造函数
    MyArray(const MyArray &arr) {
        cout << "MyArray 拷贝函数调用" << endl;
        this->m_Size = arr.m_Size;
        this->m_Capacity = arr.m_Capacity;
//        this->address = arr.address;//这就是浅拷贝。会在析构中删除,
        this->address = new T[arr.m_Capacity];//这里是为了解决浅拷贝的问题。
        for (int i = 0; i < this->m_Size; i++) {
            this->address[i] = arr.address[i];//这里如果在传递过来的arr 中,如果有其他数据,需要逐一进行拷贝过来。
        }
    }

    //重载operator=,注意这里需要返回MyArray&,返回这个可以直接进行链式赋值。
    MyArray &operator=(const MyArray &arr) {
        cout << "MyArray 赋值函数调用" << endl;
        this->m_Size = arr.m_Size;
        this->m_Capacity = arr.m_Capacity;
        if (this->address != NULL) {
            delete[]this->address;
            this->address = NULL;
            m_Size = 0;
            m_Capacity = 0;
        }
        this->address = new T[arr.m_Capacity];
        for (int i = 0; i < this->m_Size; i++) {
            this->address[i] = arr.address[i];//这里如果在传递过来的arr 中,如果有其他数据,需要逐一进行拷贝过来。
        }
        return *this;
    }

private:
    T *address;//用来维护数组里面的数据。
    int m_Size;//当前数组的大小。
    int m_Capacity;//数组的容量。
};

#endif //CHAPTER6_MYARRAY_HPP

测试代码如下:

#include "MyArray.hpp"

void test(){//测试代码
    MyArray<int>arry1 (5);
    MyArray<int>arry2(arry1);
    MyArray<int>arry3(200);
    arry1 = arry3;
//    以上的输出如下:
//    MyArray 构造函数调用
//    MyArray 拷贝函数调用
//    MyArray 构造函数调用
//    MyArray 赋值函数调用
//    MyArray 析构函数调用
//    MyArray 析构函数调用
//    MyArray 析构函数调用

}

void myPrint(MyArray<int> &arry1){
    for (int i=0;i<arry1.getArraySize();i++){
        cout << arry1[i]<<endl;
    }
}


void test1(){
    MyArray<int>arry1 (5);
    for (int i = 0;i<3;i++){
        arry1.push_back(i);
    }
    myPrint(arry1);
    cout << "删除前容量:"<<arry1.getCapacity()<<endl;//5
    cout << "删除前大小:"<<arry1.getArraySize()<<endl;//3
    arry1.pull_back();
    cout << "删除后容量:"<<arry1.getCapacity()<<endl;//5
    cout << "删除后大小:"<<arry1.getArraySize()<<endl;//2
}

class Person{
public:
    Person(){//这个必须存在,类模版初始化的时候需要。
    }
    Person(string name,int age){
       this->m_Name = name;
       this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};

//测试自定义数据类型
void test2(){
    MyArray<Person>arry(5);
    Person p1("sheik1",1);
    Person p2("sheik2",2);
    arry.push_back(p1);
    arry.push_back(p2);
    for (int i=0;i<arry.getArraySize();i++){
        cout << "姓名: "<< arry[i].m_Name<<" 年龄: "<<arry[i].m_Age<<endl;
    }
    cout << "删除前容量:"<<arry.getCapacity()<<endl;
    cout << "删除前大小:"<<arry.getArraySize()<<endl;
    arry.pull_back();
    cout << "删除后容量:"<<arry.getCapacity()<<endl;
    cout << "删除后大小:"<<arry.getArraySize()<<endl;
}
上一篇 下一篇

猜你喜欢

热点阅读