温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

数据结构(04)_数组类的实现

发布时间:2020-07-09 09:55:43 来源:网络 阅读:396 作者:三九感冒灵 栏目:编程语言

C++中支持原生数组,但由于原生数组的天然缺陷(不能获取长度信息、越界访问不会报错...),我们有必要来开发自己的数组类,从而解决这些问题。
数组类的继承关系如图:
数据结构(04)_数组类的实现

1.数组类的实现_1

1.1.抽象类模板Array

需求分析:
1、由于线性表,不能作为数组直接使用,我们需要自己实现一个数组类来代替原生数组。
2、解决原生数组越界访问不会报错的问题
3、提供数组的长度信息

1.2.Array设计要点:

  • 抽象类模本,存储空间的位置和大小由子类指定。
  • 重载数组操作符,并判断访问下标是否越界(合法)
  • 提供数组长度信息的抽象访问函数
  • 提供数组对象间的复制操作(通过重载拷贝构造函数和赋值操作符完成)
    template < typename T >
    class Array : public Object
    {
    protected:
    T *m_array;
    public:
    T& operator [] (int index)
    T operator [] (int index) const
    bool get(int index, const T& e)
    bool set(int index, const T& e)
    virtual int length(void) = 0;
    };

    1.3. Array的实现

template < typename T >
class Array : public Object
{
protected:
    T *m_array;
public:
    T& operator [] (int index)
    {
        if( (index>=0) && (index<length()) )
        {
            return m_array[index];
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "array index out of range...");
        }
    }

    T operator [] (int index) const
    {
        return const_cast<Array<T>&>(*this)[index];
    }

    bool get(int index, const T& e)
    {
        bool ret = (index>=0) && (index<length());

        if( ret )
        {
            e = m_array[index];
        }

        return ret;
    }

    bool set(int index, const T& e)
    {
        bool ret = (index>=0) && (index<length);

        if( ret )
        {
            m_array[index] = e;
        }

        return ret;
    }

    virtual int length(void) = 0;
};

1.4.StaticArray设计要点

设计要点:

  • 封装原生数组;
  • 使用模板参数决定数组大小
  • 实现函数返回数组长度
  • 拷贝构造和赋值重载
    template < typename T, int N >
    class StaticArray : public Array<T>   
    protected:
    T m_space[N];
    public:
    StaticArray()
    // 提供拷贝构造喊赋值重载函数,实现数组的拷贝
    StaticArray(const StaticArray<T, N>& obj)
    T& operator = (const StaticArray<T, N>& obj)
    int length(void)
    };

    1.5. StaticArray的实现

template <typename T, int N>
class StaticArray : public Array<T>
{
protected:
    T m_space[N];
public:
    StaticArray()
    {
        this->m_array = m_space;
    }

    StaticArray(const StaticArray<T, N>& obj)
    {
        this->m_array = m_space;

        for(int i=0; i<length();i++)   // 数组元素拷贝
        {
            m_space[i] = obj.m_space[i];
        }
    }

    T& operator ==(const StaticArray<T, N>& obj)
    {
        if(this != &obj)
        {
            this->m_array = m_space;

            for(int i=0; i<length();i++)
            {
                m_space[i] = obj.m_space[i];
            }
        }
    }

    int length(void)
    {
        return N;
    }
};

2.数组类的实现_2

2.1.DynamicArray设计要点

设计要点:类模板

  • 动态确定内部数组空间的大小
  • 实现函数返回数组的长度
  • 拷贝构造和赋值操作
    template < typename T >
    class DynamicArray : public Array<T>
    {
    protected:
    int m_length;
    public:
    DynamicArray(int length)
    DynamicArray(const DynamicArray<T>& obj)
    DynamicArray<T>& operator = (const DynamicArray<T>& obj)
    void resize(int length)
    ~DynamicArray()
    };

    2.2.DynamicArray代码优化

    DynamicArray类中的函数实现存在重复的逻辑,可以进行代码优化。
    重复代码逻辑的抽象:
    — init 函数中实现对象构造时的初始化操作
    — copy 函数负责从堆空间中申请内存,并执行拷贝构造操作
    — updata 将指定的堆空间作为内部存储数组使用

    2.3. DynamicArray实现

template <typename T>
class DynamicList : public SeqList<T>
{

protected:
    int m_capacity;
public:
    DynamicList(int capacity)
    {
        this->m_array = new T[capacity];
        if(this->m_array != NULL)
        {
            this->m_length = 0;
            this->m_capacity = capacity;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
        }
    }

    int capacity()const
    {
        return m_capacity;
    }

    void resize(int capacity)
    {
        if(capacity != m_capacity)
        {
            T* array = new T[capacity];
            if(array != NULL)
            {
                int length = (this->m_length < capacity ? this->m_length : capacity);
                for(int i=0;i<length;i++)
                {
                    array[i] = this->m_array[i];
                }

                T* temp = this->m_array;
                this->m_array = array;
                this->m_length = length;
                this->m_capacity = capacity;
                delete[] temp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
            }
        }
    }

    ~DynamicList()
    {
        delete[] this->m_array;
    }
};

总结:

  • StaticArray通过封装原生数组的方式,实现数组类
  • DynamicArray动态申请堆空间,使得数组长度动态可变
  • 数组对象能够代替原生数组,并且使用上更安全
  • 代码优化时项目开发必不可少的环节
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI