C/C++教程

kx000002-线性表-002-自定义数组类-用C++实现

本文主要是介绍kx000002-线性表-002-自定义数组类-用C++实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1. 以下是主函数测试文件:main.cpp
    #include"myArray.hpp"
    
    void testArray()
    {
        myArray<int> arr(5);    // 测试有参构造函数
        myArray<int> arr1;        // 测试无参构造函数
        arr.input(0);            // 测试输入函数
        myArray<int> arr2(arr);    // 测试拷贝构造函数
        arr1 = arr;                // 测试等号重载    
        arr.insert(3, 100);        // 测试插入函数
        arr.push_front(200);    // 测试头插函数
        arr.push_back(300);        // 测试尾插函数
        arr.push_back(500);
        arr.output();            // 测试输出函数
        arr.remove(3, NULL);    // 测试删除函数
        arr.pop_front();        // 测试头删函数
        arr.pop_back();            // 测试尾删函数
        cout << arr.locate(300) << endl;// 测试locate
        arr.setX(5, 1000);        // 测试setX
        int ret = 0;
        arr.getX(5, ret);        // 测试getX
        cout << "arr[]=" << arr[10] << endl;
        cout << "ret=" << ret << endl;
        cout << "长度是=" << arr.size() << endl;
        cout << "容量是=" << arr.capacity() << endl;
        cout << "是否为空=" << arr.empty() << endl;
        cout << "是否为满=" << arr.full() << endl;
        
        arr.output();            // 测试输出函数
    }
    int main()
    {
        testArray();
        system("pause");
        return 0;
    }

     

  2. 以下是头文件实现函数:myArray.hpp
    #pragma once
    
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    
    using namespace std;
    
    template<class T>
    class myArray
    {
    private:
        T* pbase_c;        // _c表示类内数据成员
        int capacity_c;
        int size_c;
    public:
        // 01-有参构造
        myArray(int capacity = 10);
        // 02-拷贝构造
        myArray(const myArray<T>& other);
        // 03-重载=号
        myArray<T>& operator = (const myArray<T>& other);
        // 04-析构函数
        ~myArray();
        // 05-求数组长度
        int size()const{    return this->size_c;    }
        // 06-求数组容量
        int capacity()const{    return this->capacity_c;    }
        // 07-判空
        bool empty()const{    return (this->size_c == 0);    }
        // 08-判满
        bool full()const{    return(this->size_c == this->capacity_c); }
        // 09-改变数组容量
        bool resize(int newCapacity);
        // 10-插入元素
        bool insert(int index, const T& x);
        // 11-输入函数
        void input(const T& flag);
        // 12-输出函数
        void output();
        // 13-头插法
        bool push_front(const T& x);
        // 14-尾插法
        bool push_back(const T& x);
        // 15-删除元素
        bool remove(int index, T* ret);
        // 16-头删法
        bool pop_front();
        // 17-尾删法
        bool pop_back();
        // 18-给定元素,定位下标
        int locate(const T& x);
        // 19-给定下标,获取元素
        bool getX(int index, T& ret);
        // 20-给定下标,修改元素
        bool setX(int index, const T& x);
        // 21-重载下标符号[]
        T& operator[] (int index)
        {
            return this->pbase_c[index];
        }// 此处下标不安全,可能越界
    };
    
    // 01-有参构造
    template<class T>
    myArray<T>::myArray(int capacity)
    {
        if (capacity > 0)    //若capacity小于0怎么处理?怎么返回?
        {
            T* ret = new T[capacity];
            if (ret != NULL)
            {
                this->capacity_c = capacity;
                this->size_c = 0;
                this->pbase_c = ret;
            }
        }
        return;
    }
    
    // 02-拷贝构造
    template<class T>
    myArray<T>::myArray(const myArray<T>& other)
    {
        this->capacity_c = other.capacity_c;
        this->size_c = other.size_c;
        this->pbase_c = new T[this->capacity_c];
        if (this->pbase_c == NULL)
        {
            return;
        }
        for (int i = 0; i < this->size_c; ++i)
        {
            this->pbase_c[i] = other.pbase_c[i];
        }
    }
    // 03-重载=号
    template<class T>
    myArray<T>&  myArray<T>::operator = (const myArray<T>& other)
    {    //若this==NULL或other==NULL该如何返回
        if (this->pbase_c != NULL)
        {
            delete this->pbase_c;    // 先释放原堆区数据
            this->pbase_c = NULL;
            this->capacity_c = 0;
            this->size_c = 0;
        }
        this->capacity_c = other.capacity_c;
        this->size_c = other.size_c;
        this->pbase_c = new T[this->size_c];
        if (this->pbase_c != NULL)
        {
            for (int i = 0; i < this->size_c; ++i)
            {
                this->pbase_c[i] = other.pbase_c[i];
            }
        }
        return *this;
    }
    
    // 04-析构函数
    template<class T>
    myArray<T>::~myArray()
    {
        if (this->pbase_c != NULL)
        {
            delete[] this->pbase_c;
            this->pbase_c = NULL;
        }
    }
    
    // 05-改变数组容量
    template<class T>
    bool myArray<T>::resize(int newCapacity)
    {
        if (newCapacity < 1)
        {
            return false;
        }
        if (newCapacity != this->capacity_c)
        {
            T* ret = new T[newCapacity];
            if (ret == NULL)
            {
                return false;
            }
            T* pdel = this->pbase_c;    // 存放原数组基地址
            int len = (this->size_c > newCapacity ? newCapacity : this->size_c);
            for (int i = 0; i < len; ++i)
            {
                ret[i] = pdel[i];
            }
            this->pbase_c = ret;
            this->capacity_c = newCapacity;
            this->size_c = len;
            delete[] pdel;
            return true;
        }
        return false;    // 若新旧数组容量一致则改变容量失败
    }
    
    // 06-插入元素
    template<class T>
    bool myArray<T>::insert(int index, const T& x)
    {
        if (this->size_c == this->capacity_c)
        {
            if (!this->resize((this->capacity_c) * 2))
            {
                return false;
            }// 扩容失败
        }
        if (index<0 || index>this->size_c)
        {
            return false;    // 插入位置不合理
        }
        for (int i = this->size_c; i > index; --i)
        {
            this->pbase_c[i] = this->pbase_c[i - 1];
        }
        this->pbase_c[index] = x;
        ++this->size_c;
        return true;
    }
    
    // 07-输入函数
    template<class T>
    void myArray<T>::input(const T& flag)
    {
        T data;    // 保存从键盘输入的元素
        cout << "请输入若干个元素(等于" << flag << "时结束输入):>" << endl;
        int index = 0;    // 数组下标
        while (cin >> data, data != flag)
        {
            if (this->size_c == this->capacity_c)
            {
                if (!this->resize((this->capacity_c) * 2))
                {
                    return;
                }    // 扩容失败
            }
            this->pbase_c[index++] = data;
            ++this->size_c;
        }
    }
    
    // 08-输出函数
    template<class T>
    void myArray<T>::output()
    {
        cout << "============================================" << endl;
        cout << "size=" << this->size_c << endl;
        cout << "capacity=" << this->capacity_c << endl;
        for (int i = 0; i < this->size_c; ++i)
        {
            cout << this->pbase_c[i] << "  ";
        }
        cout << endl;
        cout << "============================================" << endl;
    }
    
    // 13-头插法 
    template<class T>
    bool myArray<T>::push_front(const T& x)
    {
        if (this->size_c == this->capacity_c)
        {
            if (!(this->resize((this->capacity_c) * 2)))
            {
                return false;
            }
        }
        for (int i = this->size_c; i > 0; --i)
        {
            this->pbase_c[i] = this->pbase_c[i - 1];
        }
        this->pbase_c[0] = x;
        ++this->size_c;
        return true;
    }
    
    // 14-尾插法
    template<class T>
    bool myArray<T>::push_back(const T& x)
    {
        if (this->size_c == this->capacity_c)
        {
            if (!(this->resize((this->capacity_c) * 2)))
            {
                return false;
            }
        }
        this->pbase_c[this->size_c++] = x;
        return true;
    }
    
    // 15-删除元素
    template<class T>
    bool myArray<T>::remove(int index, T* ret)
    {
        if (this->size_c == 0 || index<0 || index>this->size_c - 1)
        {
            return false;
        }
        if (ret != NULL)
        {
            *ret = this->pbase_c[index];
        }// 用ret接收被删除的元素值
        for (int i = index; i < this->size_c - 1; ++i)
        {
            this->pbase_c[i] = this->pbase_c[i + 1];
        }
        --this->size_c;
        return true;
    }
    
    // 16-头删法
    template<class T>
    bool myArray<T>::pop_front()
    {
        if (this->size_c == 0)
        {
            return false;
        }
        for (int i = 0; i < this->size_c - 1; ++i)
        {
            this->pbase_c[i] = this->pbase_c[i + 1];
        }
        --this->size_c;
        return true;
    }
    
    // 17-尾删法
    template<class T>
    bool myArray<T>::pop_back()
    {
        if (this->size_c > 0)
        {
            --this->size_c;
            return true;
        }
        return false;
    }
    
    // 18-给定元素,定位下标
    template<class T>
    int myArray<T>::locate(const T& x)
    {
        if (this->size_c == 0)
        {
            return -1;
        }
        for (int i = 0; i < this->size_c; ++i)
        {
            if (this->pbase_c[i] == x)
            {
                return i;
            }
        }
        return -1;
    }
    
    // 19-给定下标,获取元素
    template<class T>
    bool myArray<T>::getX(int index, T& ret)
    {
        if (this->size_c == 0 || index<0 || index>this->size_c - 1)
        {
            return false;
        }
        ret = this->pbase_c[index];
        return true;
    }
    
    // 20-给定下标,修改元素
    template<class T>
    bool myArray<T>::setX(int index, const T& x)
    {
        if (this->size_c == 0 || index<0 || index>this->size_c - 1)
        {
            return false;
        }
        this->pbase_c[index] = x;
        return true;
    }

     

这篇关于kx000002-线性表-002-自定义数组类-用C++实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!