数据结构 第20课 数组类的创建(下)------------------狄泰软件学院

前辈的文章奉上

DynamicArray.h

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

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

namespace DTLib
{
    
    
    /********************** 首次实现 ***************************************/

/*   */
template < typename T >
class DynamicArray : public Array<T>
{
    
    
protected:
    int m_length;                        // 当前数组元素个数
public:
    DynamicArray(int length)             // 构造对象时就要指定,当前数组的初始大小
    {
    
    
        this->m_array = new T[length];

        if(this->m_array != NULL)
        {
    
    
            this->m_length = length;     // 内存申请成功,记录当前数组的长度
        }
        else
        {
    
    
            THROW_EXCEPTION(NoEnoughMemoryException, "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<obj.m_length; i++)
            {
    
    
                this->m_array[i] = obj.m_array[i];  // 拷贝
            }
        }
        else
        {
    
    
            THROW_EXCEPTION(NoEnoughMemoryException, "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;   // temp 是赋值操作前this->m_array所指向的空间,,意义是  : 异常安全

            }
            else
            {
    
    
                THROW_EXCEPTION(NoEnoughMemoryException, "No memory to copy Object...");
            }
        }
        return *this;

    }

    int length() const
    {
    
    
        return m_length;
    }

    void resize(int length)
    {
    
    
        if( length != m_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 memory to resize Object...");

            }
        }
    }

    ~DynamicArray()
    {
    
    
        delete[] this->m_array;
    }
};
#endif // DYNAMICARRAY_H

main.cpp

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


using namespace std;
using namespace DTLib;


int main()
{
    
    

    DynamicArray<int> s1(5);

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

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

    DynamicArray<int> s2(10);

    s2 = s1;

    s2.resize(3);

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

    s2[4] = 5;


    return 0;
}

DynamicArray.h (优化)

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

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

namespace DTLib
{
    
    
    /********************代码优化:重构DynamicArray.h****************************/


template < typename T >
class DynamicArray : public Array<T>
{
    
    
protected:
    int m_length;                        // 当前数组元素个数

    //1、在堆空间中申请内存, 2、执行拷贝工作
    T* copy(T* array, int len, int newLen)  // O(min(len,newLen)) ==> O(n)   n 为元素的个数
    {
    
    
        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(NoEnoughMemoryException, "No memory to update DynamicArray Object...");
        }
    }

        // 对象构造时初始化操作(设置两个成员变量的值)
         void init(T* array, int length)  
         {
    
    
             if( array != NULL )
             {
    
    
                 this->m_array = array;
                 this->m_length = length;
             }
             else
             {
    
    
                 THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicArray Object...");
             }
         }


public:
    DynamicArray(int length)             // 构造对象时就要指定,当前数组的初始大小
    {
    
    

        init(new T[length], length);     // 先申请一个数组, 然后设置进去就可以了

        /*
        this->m_array = new T[length];

        if(this->m_array != NULL)
        {
            this->m_length = length;     // 内存申请成功,记录当前数组的长度
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicArray Object...");
        }
        */
    }


    // 拷贝构造函数
    DynamicArray(const DynamicArray<T>& obj)
    {
    
    
        // 优化:
        //T* array = copy(obj.m_array, obj.m_length, obj.m_length);
        //init(array, obj.m_length);
        // ===> 两句合成一句
        init(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);   // O(n)


        /*
        this->m_array = new T[obj.m_length];        // 申请的 数组的长度是参数对象的长度

        if(this->m_array != NULL)
        {
            this->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 memory to create DynamicArray Object...");
        }
        */
    }


    DynamicArray<T>& operator =(const DynamicArray<T>& obj) // O(n)
    {
    
    
        if( this != &obj )
        {
    
    

            // 优化:
            //T* array = copy(obj.m_array, obj.m_length, obj.m_length);  // 当前所作工作是复制的工作
            //update(array, obj.m_length);
            // ===> 两句合成一句
            update(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);


            /* 原函数
            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;   // temp 是赋值操作前this->m_array所指向的空间,,意义是  : 异常安全

            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No memory to copy Object...");
            }
            */
        }
        return *this;

    }

    int length() const
    {
    
    
        return m_length;
    }

    void resize(int length)  // O(n)
    {
    
    
        if( length != m_length )
        {
    
    
            // 优化:
            // T* array = copy(this->m_array, m_length, length);  // 拷贝的原数组是this->m_array成员指针所指向的数组, 新的数组大小是length
            // update(array, length);
            // ===> 两句合成一句
            update(copy(this->m_array, m_length, 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 memory to resize Object...");

            }
            */
        }
    }

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


/********************完成·重构****************************/

#endif // DYNAMICARRAY_H

main.cpp (优化)


/*
* 时间:2021/3/13
*  		(未解决)报错:expected '}' at end of input  
*		测试未通过
* 时间:2021/3/14   
*		方案:重新输入一遍程序后,报错解决,全部通过;
*	
*/
#include <iostream>
#include "DynamicArray.h"


using namespace std;
using namespace DTLib;


int main()
{
    
    

    DynamicArray<int> s1(5);

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

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

    DynamicArray<int> s2(10);

    s2 = s1;

    s2.resize(3);

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

    return 0;
}

猜你喜欢

转载自blog.csdn.net/dashuu/article/details/114753404