欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 動態(tài)數(shù)組模版類Vector實例詳解

 更新時間:2022年02月25日 16:00:43   作者:諾謙  
這篇文章主要為大家詳細介紹了C++動態(tài)數(shù)組模版類Vector實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.實現(xiàn)機制

內(nèi)部主要通過m_capacity數(shù)組容量成員和m_length數(shù)組有效長度成員來維護一個T* data數(shù)組空間.

內(nèi)部默認分配一定數(shù)量大小的數(shù)組指針,每次append尾部追加的時候,無需再次分配空間,直接賦值標志length長度,假如超過當前空間容量,則再次擴大分配新的內(nèi)存數(shù)組,并將舊數(shù)組拷貝至新數(shù)組及釋放舊數(shù)組.

Vector需要實現(xiàn)的public函數(shù)如下所示:

  • inline int capacity() 獲取容量
  • inline int length() : 獲取有效長度
  • void resize(int asize) : 改變數(shù)組的有效長度
  • void append(const T &t) : 尾部追加一個元素
  • T& operator[] (int i) : 通過[]獲取元素
  • T operator[] (int i) const : 通過[]獲取常量元素
  • void clear() :清空數(shù)組中的數(shù)據(jù)
  • inline bool isEmpty(): 數(shù)組是否有數(shù)據(jù)

resize()函數(shù)實現(xiàn)細節(jié):

  • 如果resize長度大于當前容量時 : 則擴大分配新的內(nèi)存數(shù)組,并將舊數(shù)組拷貝至新數(shù)組及釋放舊數(shù)組.
  • 如果resize長度小于當前l(fā)ength時 : 則需要將多余的成員進行釋放,調(diào)用析構(gòu)函數(shù)實現(xiàn).
  • 如果resize長度大于當前l(fā)ength時 : 則需要調(diào)用默認構(gòu)造函數(shù)來填充內(nèi)部數(shù)組.

2.代碼實現(xiàn)

#ifndef VECTOR_H
#define VECTOR_H
#include "throw.h"
// throw.h里面定義了一個ThrowException拋異常的宏,如下所示:
//#include <iostream>
//using namespace std;
//#define ThrowException(errMsg)  {cout<<__FILE__<<" LINE"<<__LINE__<<": "<<errMsg<<endl; (throw errMsg);}
template <typename T>
class Vector
{
    T* m_data;
    int m_length;       // 有效數(shù)據(jù)的長度
    int m_capacity;     // 分配容量的長度
    // 分配
    T* allocate(int size)
    {
        T* arr = new T[size];
        if(arr == NULL) {
            ThrowException( "No memory to create DynamicArray object ...");
        }
        return arr;
    }
    // 重新分配
    void realloc(int capacity)
    {
        T* newData = allocate(capacity);
        for(int i=0; i<m_length; i++) {
            newData[i] = m_data[i];
        }
        delete[] m_data;
        m_data = newData;
        m_capacity = capacity;
    }
    // 調(diào)用析構(gòu)函數(shù)
    void destruct(int from, int end)
    {
        while(from++<end) {
           m_data[from].~T();
        }
    }
    // 調(diào)用默認構(gòu)造函數(shù)
    void defaultConstruct(int from, int end)
    {
        while(from++<end) {
           m_data[from] = T();
        }
    }
public:
    Vector(int lenght = 50)  { m_length = 0; m_data = allocate(lenght); m_capacity = lenght; }
    inline int capacity() const { return m_capacity; }     // 獲取容量
    inline int size()  { return m_length; }         // 獲取有效長度
    inline int length()  { return size(); }
    inline T *data() {  return m_data; }
    inline const T *data() const { return m_data; }
    inline bool isEmpty() const { return m_length == 0; }
    void clear()
    {
        if(!m_length) return;
        destruct(0, m_length);
        m_length = 0;
    }
    void resize(int asize)
    {
        if(asize == m_length) return;
        // 重新分配的大小>當前容量時
        if(asize > m_capacity) {
            realloc(asize);
        }
        if (asize < m_length)    // 分配的大小<當前大小時,則調(diào)用析構(gòu)
            destruct(asize, m_length);
        else        // 分配的大小>當前大小時,則調(diào)用默認構(gòu)造
            defaultConstruct(m_length, asize);
        m_length = asize;
    }
    // 尾部追加一個元素
    void append(const T &t)
    {
        if(m_length == m_capacity) {
            realloc(m_capacity+20);     // 如果容量滿了,則默認增加20個容量.方便后面append無需再次分配內(nèi)存
        }
        m_data[m_length] = t;
        m_length++;
    }
    T& operator[] (int i)
    {
        if((0 <= i) && (i < length()))
        {
            return m_data[i];
        }
        else
        {
            ThrowException("Parameter i is invalid ...");
        }
    }
    T operator[] (int i) const
    {
        return m_data[i];
    }
};
#endif // VECTOR_H

3.測試運行

測試如下所示:

class Test {
public:
    int number;
    Test(int n = 0) {
        number = n;
    }
};
int main(int argc, char *argv[])
{
   Vector<Test> arr;
   for(int i = 0; i < 10; i++)
       arr.append(Test(i));
   cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
   for(int i = 0; i < arr.length(); i++)
       cout<<"arr []:"<<arr[i].number<<endl;
   cout<<"*********"<<endl;
   arr.resize(13);
   cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
   for(int i = 0; i < arr.length(); i++)
       cout<<"arr []:"<<arr[i].number<<endl;
   cout<<"*********"<<endl;
   arr.resize(5);
   cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
   for(int i = 0; i < arr.length(); i++)
       cout<<"arr []:"<<arr[i].number<<endl;
   cout<<"*********"<<endl;
    return 0;
}

運行如下所示:

可以看到我們resize(13)后,由于 resize長度大于當前arr的length,所以則調(diào)用默認構(gòu)造函數(shù)來填充內(nèi)部數(shù)組.所以arr[10]至arr[12]的number為0。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!  

相關(guān)文章

  • C語言學生信息管理系統(tǒng)小項目

    C語言學生信息管理系統(tǒng)小項目

    這篇文章主要為大家詳細介紹了C語言學生信息管理系統(tǒng)小項目,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 基于C語言實現(xiàn)簡易三子棋游戲

    基于C語言實現(xiàn)簡易三子棋游戲

    這篇文章主要為大家詳細介紹了基于C語言實現(xiàn)簡易三子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>
    2022-01-01
  • C語言中求解圖形的問題

    C語言中求解圖形的問題

    這篇文章主要介紹了C語言中求解圖形的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++ 中dynamic_cast&lt;&gt;的使用方法小結(jié)

    C++ 中dynamic_cast&lt;&gt;的使用方法小結(jié)

    將一個基類對象指針(或引用)cast到繼承類指針,dynamic_cast會根據(jù)基類指針是否真正指向繼承類指針來做相應處理
    2013-03-03
  • 基于OpenCV實現(xiàn)圖像分割

    基于OpenCV實現(xiàn)圖像分割

    這篇文章主要為大家詳細介紹了基于OpenCV實現(xiàn)圖像分割,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C 語言中實現(xiàn)環(huán)形緩沖區(qū)

    C 語言中實現(xiàn)環(huán)形緩沖區(qū)

    本文主要是介紹 C語言實現(xiàn)環(huán)形緩沖區(qū),并附有詳細實現(xiàn)代碼,具有一定的參考價值,希望能幫助有需要的小伙伴
    2016-07-07
  • c/c++小游戲源代碼

    c/c++小游戲源代碼

    這篇文章主要介紹了c/c++小游戲源代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • c語言 兩字符串交叉合并實例

    c語言 兩字符串交叉合并實例

    今天小編就為大家分享一篇c語言 兩字符串交叉合并實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C++OOP對象和類的詳細講解

    C++OOP對象和類的詳細講解

    這篇文章主要介紹了C++面相對象編程中的類與對象的特性與概念,OOP面向?qū)ο笳Z言相對C語言這樣面相過程的語言來說具有類和對象以及方法這樣的特性,需要的朋友可以參考下
    2021-08-08
  • C++中spdlog的簡單使用示例

    C++中spdlog的簡單使用示例

    spdlog是一個開源、跨平臺、無依賴、只有頭文件的C++11日志庫,所以這篇文章主要來和大家介紹一下一個簡單的spdlog使用示例,感興趣的小伙伴可以了解一下
    2023-08-08

最新評論