数组类—DynamicArray

DynamicArray


上一篇说到了数组类的一种具体对象,这篇里面来说另一种。就是DynamicArray,既然StaticArray已经够好了,为什么还要有DynamicArray呢。就如同前几片博客里讲的基于顺序存储结构的线性表的两种实现一样,我们想让这个对象可以动态的改变自己的存储空间的大小,这样就会更加灵活。


设计要点:

类模板
在堆中申请一定的空间作为存储空间
实现函数返回长度
拷贝构造和赋值
动态确定内部数组空间


程序表现:

template <typename T>
class DynamicArray : public Array<T>
{
protected:
    int m_length;

    T* copy(T* array, int len, int newlen);
    void update(T* array, int length);

public:
    DynamicArray(int length);

    DynamicArray(const DynamicArray<T>& obj);

    int length() const;

    void resize(int length);

    DynamicArray<T>& operator = (const DynamicArray<T>& obj);

    ~DynamicArray();

};


代码实现:

template <typename T>
class DynamicArray : public Array<T>
{
protected:
    int m_length;

    T* copy(T* array, int len, int newlen)
    {
        T* ret = new T[newlen];

        if(ret != NULL)
        {
            int size = (len < newlen) ? len : newlen;

            for(int i=0; i<size; i++)
            {
                ret[i] = array[i];
            }
        }

        return ret;
    }

    void update(T* array, int length)
    {
        if(array != NULL)
        {
            T* temp = this->m_array;

            this->m_array = array;
            this->m_length = length;

            delete[] temp;
        }
    }

public:
    DynamicArray(int length)
    {
        this->m_array = new T[length];

        if( this->m_array != NULL)
        {
            m_length = length;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException,"No enough memory to creat DynamicArray!");
        }
    }

    DynamicArray(const DynamicArray<T>& obj)
    {
        this->m_array = new T[obj.m_length];

        if( this->m_array != NULL)
        {
            m_length = obj.m_length;

            for(int i=0; i<obj.m_length; i++)
            {
                this->m_array[i] = obj.m_array[i];
            }
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException,"No enough memory to creat DynamicArray!");
        }
    }

    int length() const
    {
        return m_length;
    }

    void resize(int length)
    {
        if(m_length != length)
        {
            T* array = new T[length];

            if( array != NULL)
            {
                int size = (length < m_length) ? length : m_length;

                for(int i=0; i<size; i++)
                {
                    array[i] = this->m_array[i];
                }

                T* temp = this->m_array;

                this->m_array = array;
                this->m_length = length;

                delete[] temp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException,"No enough memory to creat DynamicArray!");
            }
        }
    }

    DynamicArray<T>& operator = (const DynamicArray<T>& obj)
    {
        if(this != &obj)
        {
            T* array = new T[obj.m_length];

            if( array != NULL)
            {
                for(int i=0; i<obj.m_length; i++)
                {
                    array[i] = obj.m_array[i];
                }

                T* temp = this->m_array;

                this->m_array = array;
                this->m_length = obj.m_length;

                delete[] temp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException,"No enough memory to copy DynamicArray!");
            }
        }

        return *this;
    }

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

};



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值