数组类的创建——DynamicArray.h

完成DynamicArray类的具体实现

DynamicArray设计要点——类模板   动态确定内部数组空间的大小   实现函数返回数组长度   拷贝构造和赋值操作

DynamicArray类的声明

template <typename T>
class DynamicArray : public Array<T>
{
protected:
    T m_length;
public:
    DynamicArray(int length);
    //拷贝构造和赋值操作 
    DynamicArray(const DynamicArray<T>& obj );
    DynamicArray<T>& operator= (const DynamicArray<T>& obj);
    
    int length() const;
    void resize(int length);  //动态重置数组的长度
    
    ~DynamicArray();
};
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include "Array.h"
#include "Exception.h"

namespace DTLib
{
template <typename T>
class DynamicArray : public Array<T>
{
protected:
    T m_length;
public:
    DynamicArray(int length)
    {
        this->m_array = new T[length];

        if(this->m_array != NULL)
        {
            this->m_length = length;
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to create DynamicArray object...");
        }
    }
    //拷贝构造和赋值操作
    DynamicArray(const DynamicArray<T>& obj )
    {
        this->m_array = new T[obj.m_length];

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

            for(int i=0; i<m_length; i++)
            {
                this->m_array[i] = obj.m_array[i];
            }
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to create DynamicArray object...");
        }
    }
    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(IndexOutOfBoundsException, "No memory to copy DynamicArray object...");
            }
        }

        return *this;
    }

    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(IndexOutOfBoundsException, "No memory to resize DynamicArray object...");
            }
        }
    }

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

}

#endif // DYNAMICARRAY_H

测试:

#include <iostream>
#include "DynamicArray.h"

using namespace std;
using namespace DTLib;



int main()
{

    DynamicArray<int> sl(5);

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

    for(int i=0; i<sl.length(); i++)
    {
        cout << sl[i] << endl;
    }

    cout << endl;
    DynamicArray <int> s2(5);
    s2 = sl;
    for(int i=0; i<s2.length(); i++)
    {
        cout << s2[i] << endl;
    }

    cout << endl;

    s2.resize(10);
    for(int i=0; i<10; i++)
    {
        cout << s2[i] << endl;
    }
    return 0;

}

代码优化:

DynamicArray类中的函数实现存在重复的逻辑,如何进行代码优化?

重复代码逻辑的抽象——init   对象构造时的初始化操作——copy   在堆空间中申请新的内存,并执行拷贝操作——update   将指定的堆空间作为内部存储数组使用

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include "Array.h"
#include "Exception.h"

namespace DTLib
{
template <typename T>
class DynamicArray : public Array<T>
{
protected:
    T 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;
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to resize DynamicArray object...");
        }
    }

    void init(T* array, int length)
    {
        if(array != NULL)
        {
            this->m_array = array;
            this->m_length = length;
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to create DynamicArray object...");
        }
    }
public:
    DynamicArray(int length)
    {
        init(new T(length),length);
    }
    //拷贝构造和赋值操作
    DynamicArray(const DynamicArray<T>& obj )
    {
        init( copy(obj.m_array,obj.m_length,obj.m_length),obj.m_length);
    }
    DynamicArray<T>& operator= (const DynamicArray<T>& obj)
    {
        if(this != &obj)
        {
            update( copy(obj.m_array,obj.m_length,obj.m_length),obj.m_length);
        }

        return *this;
    }

    int length() const
    {
        return m_length;
    }
    void resize(int length) //动态重置数组的长度
    {
        if(m_length != length)
        {
            update(copy(this->m_array, m_length ,length),length);
        }
    }

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

}

#endif // DYNAMICARRAY_H

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态数组DynamicArray)是一种可以动态增加或减少长度的数组。它的长度可以根据需要进行自动扩展或收缩。动态数组通常用于需要频繁插入、删除或者动态调整大小的场合。 动态数组的实现基于静态数组,通过动态申请新的内存空间,将原有的数据复制到新的内存空间中来实现自动扩展或收缩。当数组长度不够时,动态数组会自动申请更大的内存空间,将原有的数据复制到新的内存空间中,并释放原有的内存空间;当数组长度过长时,动态数组会自动收缩内存空间,以节省内存使用。 动态数组的常见操作包括随机访问、插入元素、删除元素、获取数组长度等。具体实现可以使用模板,支持不同型的数据。以下是一个简单的动态数组的示例代码: ```cpp template<typename T> class DynamicArray { private: T* data; int capacity; // 当前内存容量 int size; // 实际元素个数 void resize(int new_capacity) { T* new_data = new T[new_capacity]; for (int i = 0; i < size; i++) { new_data[i] = data[i]; } delete[] data; data = new_data; capacity = new_capacity; } public: DynamicArray() { data = new T[1]; capacity = 1; size = 0; } ~DynamicArray() { delete[] data; } int getSize() { return size; } T& operator[](int index) { if (index < 0 || index >= size) { throw "Index out of range."; } return data[index]; } void add(T item) { if (size == capacity) { resize(capacity * 2); } data[size++] = item; } void insert(int index, T item) { if (index < 0 || index > size) { throw "Index out of range."; } if (size == capacity) { resize(capacity * 2); } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = item; size++; } void remove(int index) { if (index < 0 || index >= size) { throw "Index out of range."; } for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; } size--; if (size <= capacity / 4) { resize(capacity / 2); } } }; ``` 这是一个基于模板实现的动态数组,支持任意型的数据。它包括了常见的数组操作,如随机访问、添加元素、插入元素、删除元素等。通过动态扩展和收缩内存空间,可以实现数组长度的动态调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值