欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

數(shù)據(jù)結(jié)構(gòu)(04)_數(shù)組類的實(shí)現(xiàn)

C++中支持原生數(shù)組,但由于原生數(shù)組的天然缺陷(不能獲取長度信息、越界訪問不會(huì)報(bào)錯(cuò)...),我們有必要來開發(fā)自己的數(shù)組類,從而解決這些問題。
數(shù)組類的繼承關(guān)系如圖:
數(shù)據(jù)結(jié)構(gòu)(04)_數(shù)組類的實(shí)現(xiàn)

創(chuàng)新互聯(lián)公司是專業(yè)的德安網(wǎng)站建設(shè)公司,德安接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行德安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

1.數(shù)組類的實(shí)現(xiàn)_1

1.1.抽象類模板Array

需求分析:
1、由于線性表,不能作為數(shù)組直接使用,我們需要自己實(shí)現(xiàn)一個(gè)數(shù)組類來代替原生數(shù)組。
2、解決原生數(shù)組越界訪問不會(huì)報(bào)錯(cuò)的問題
3、提供數(shù)組的長度信息

1.2.Array設(shè)計(jì)要點(diǎn):

  • 抽象類模本,存儲(chǔ)空間的位置和大小由子類指定。
  • 重載數(shù)組操作符,并判斷訪問下標(biāo)是否越界(合法)
  • 提供數(shù)組長度信息的抽象訪問函數(shù)
  • 提供數(shù)組對象間的復(fù)制操作(通過重載拷貝構(gòu)造函數(shù)和賦值操作符完成)
    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的實(shí)現(xiàn)

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設(shè)計(jì)要點(diǎn)

設(shè)計(jì)要點(diǎn):

  • 封裝原生數(shù)組;
  • 使用模板參數(shù)決定數(shù)組大小
  • 實(shí)現(xiàn)函數(shù)返回?cái)?shù)組長度
  • 拷貝構(gòu)造和賦值重載
    template < typename T, int N >
    class StaticArray : public Array<T>   
    protected:
    T m_space[N];
    public:
    StaticArray()
    // 提供拷貝構(gòu)造喊賦值重載函數(shù),實(shí)現(xiàn)數(shù)組的拷貝
    StaticArray(const StaticArray<T, N>& obj)
    T& operator = (const StaticArray<T, N>& obj)
    int length(void)
    };

    1.5. StaticArray的實(shí)現(xiàn)

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++)   // 數(shù)組元素拷貝
        {
            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.數(shù)組類的實(shí)現(xiàn)_2

2.1.DynamicArray設(shè)計(jì)要點(diǎn)

設(shè)計(jì)要點(diǎn):類模板

  • 動(dòng)態(tài)確定內(nèi)部數(shù)組空間的大小
  • 實(shí)現(xiàn)函數(shù)返回?cái)?shù)組的長度
  • 拷貝構(gòu)造和賦值操作
    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代碼優(yōu)化

    DynamicArray類中的函數(shù)實(shí)現(xiàn)存在重復(fù)的邏輯,可以進(jìn)行代碼優(yōu)化。
    重復(fù)代碼邏輯的抽象:
    — init 函數(shù)中實(shí)現(xiàn)對象構(gòu)造時(shí)的初始化操作
    — copy 函數(shù)負(fù)責(zé)從堆空間中申請內(nèi)存,并執(zhí)行拷貝構(gòu)造操作
    — updata 將指定的堆空間作為內(nèi)部存儲(chǔ)數(shù)組使用

    2.3. DynamicArray實(shí)現(xiàn)

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;
    }
};

總結(jié):

  • StaticArray通過封裝原生數(shù)組的方式,實(shí)現(xiàn)數(shù)組類
  • DynamicArray動(dòng)態(tài)申請堆空間,使得數(shù)組長度動(dòng)態(tài)可變
  • 數(shù)組對象能夠代替原生數(shù)組,并且使用上更安全
  • 代碼優(yōu)化時(shí)項(xiàng)目開發(fā)必不可少的環(huán)節(jié)

名稱欄目:數(shù)據(jù)結(jié)構(gòu)(04)_數(shù)組類的實(shí)現(xiàn)
網(wǎng)站URL:http://aaarwkj.com/article34/jjgcse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、、標(biāo)簽優(yōu)化企業(yè)網(wǎng)站制作、做網(wǎng)站微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
国产精品国产亚洲精品| 国产精品国产亚洲精品看不| 欧美一级纯黄电影视频| 在线欧美亚洲观看天堂| 亚洲日本熟妇在线视频| 中文字幕一区二区av| 91高清视频在线免费观看| 精品人妻中文字幕在线| 国内自拍一区二区三区| 免费女性啪啪无遮挡网站| 欧美亚洲另类在线日韩国产| 亚洲性图中文字幕在线| 97久久久人妻精品一区| 欧美国产精品一区二区免费| 亚洲精品日韩国产av| 亚洲中文字幕一区乱码| 亚洲黄色暴力一区视频| 亚洲男人天堂免费观看| 中文字幕一区二区av| 亚洲奇米精品一区二区| 欧美日韩国产综合在线观看| 欧美一区二区三区va| 久久亚洲中文字幕精品一区四区| 日本一区不卡二区高清| 亚洲国产香蕉视频在线播放| 日韩欧美一区二区不卡在线| 精品一区精品二区国产日韩| 日韩人妻精品在线一区二区 | 日韩精品高清中文字幕| 国产青草视频免观看视频| 伊人婷婷综合激情网| 国产好大好爽在线免费观看| 国产亚洲一区二区视频| av一区二区三区网站| 欧美日韩国产亚洲免费| 日韩中文不卡人成在线视频| 免费亚洲一区二区三区| 免费在线观看av不卡| 日韩精品国产一区二区在线| 免费国产污在线观看网站| 国产亚洲精品久在线|