zoukankan      html  css  js  c++  java
  • c++之类模板案例

    描述:使用一个通用的数组类,要求如下:

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

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

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

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

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

    6.可通过下标进行访问数组中的元素;

    7.可以获得数组中当前元素的个数以及容量;

    头文件:MyArray.hpp

    #include<iostream>
    using namespace std;
    
    template<class T>
    class MyArray {
    public:
        //有参构造
        MyArray(int capacity) {
            cout << "有参构造函数调用" << endl;
            this->m_Capacity = capacity;
            this->m_Size = 0;
            this->pAddress = new T[this->m_Capacity];
        } 
        //析构函数
        ~MyArray() {
            cout << "析构函数调用" << endl;
            if (this->pAddress != NULL) {
                delete[] this->pAddress;
                this->pAddress = NULL;
            }
        }
        //拷贝构造
        MyArray(const MyArray& arr) {
            cout << "拷贝构造函数调用" << 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 < this->m_Size; i++) {
                this->pAddress[i] = arr.pAddress[i];
            }
        }
        //operator=也是为了防止浅拷贝问题
        MyArray& operator=(const MyArray& arr) {
            cout << "重载=调用" << endl;
            //先判断原来的堆区是否有数据,如果有先释放
            if (this->pAddress != NULL) {
                delete[] this->pAddress;
                this->pAddress = NULL;
                this->m_Size = 0;
                this->m_Capacity = 0;
            }
            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--;
        }
        //通过数组下标访问数据
        T& operator[](int index) {
            return this->pAddress[index];
        }
        //返回数组的容量
        int getCapacity() {
            return this->m_Capacity;
        }
        //返回数组的大小
        int getSize() {
            return this->m_Size;
        }
    private:
        T* pAddress;//指针指向堆区开辟的真实数组
        int m_Capacity;//容量
        int m_Size;//数组大小
    
    };

    源文件:test.cpp

    #include "myArray.hpp"
    
    void printArr(MyArray <int>& arr) {
        for (int i = 0; i < arr.getSize(); i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
    
    void test() {
        MyArray <int>arr1(5);
        //MyArray <int>arr2(arr1);
        //MyArray <int>arr3(10);
        //arr3 = arr1;
        //插入数据
        arr1.Push_Back(1);
        arr1.Push_Back(2);
        cout << "插入之后的数组:" << endl;
        printArr(arr1);
        cout << "删除最后一个元素的数组:" << endl;
        arr1.Pop_back();
        printArr(arr1);
        cout << "此时数组的大小:" << arr1.getSize()<<endl;
        cout << "此时数组的容量:" << arr1.getCapacity() << endl;
    
    }
    
    int main() {
        test();
        system("pause");
        return 0;
    }

    运行结果:

  • 相关阅读:
    面试精选:链表问题集锦
    经典排序算法总结与实现 ---python
    Python高级编程–正则表达式(习题)
    Python面试题汇总
    Python正则表达式
    Linux下的Libsvm使用历程录
    在 linux(ubuntu) 下 安装 LibSVM
    过拟合
    百度历年笔试面试150题
    MATLAB 的数据类型
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12108584.html
Copyright © 2011-2022 走看看