C++泛型動態(tài)數(shù)組實現(xiàn)

實現(xiàn)了一個可自動擴充容量的泛型數(shù)組。

頭文件:Array.h

#ifndef Array_hpp
#define Array_hpp

template <class T>
class Array{
private:
    T *base;        //數(shù)組首地址
    int length;     //數(shù)組中元素
    int size;       //數(shù)組大小,以數(shù)組中元素的大小為單位
public:
    //初始化數(shù)組,分配內(nèi)存
    bool init();
    //檢查內(nèi)存是否夠用,不夠用就增加
    bool ensureCapcity();
    //添加元素到數(shù)組尾
    bool add(T item);
    //插入元素到數(shù)組的具體位置,位置從1開始
    bool insert(int index,T item);
    //刪除指定位置的元素并返回,位置從1開始
    T del(int index);
    //返回指定位置的元素
    T objectAt(int index);
    //打印數(shù)組所有元素
    void display();
};

#endif /* Array_hpp */

實現(xiàn):Array.cpp

#include "Array.hpp"
#include <mm_malloc.h>
#include <iostream>
using namespace std;

template<typename T> bool Array<T>::init(){    
    base = (T *)malloc(10*sizeof(T));
    if(!base){
        return false;
    }
    size = 10;
    length = 0;
    return true;
}

template<typename T> bool Array<T>::ensureCapcity(){
    if(length >= size){
        T *newBase = (T*)realloc(base,10 * sizeof(T) + size);
        if(!newBase){
            return false;
        }
        base = newBase;
        size += 10;
        newBase = nullptr;
    }
    return true;
}

template<typename T> bool Array<T>::add(T item){
    if(!ensureCapcity()){
        return false;
    }
    T *p = base + length;
    *p = item;
    length ++;
    return true;
}

template<typename T> bool Array<T>::insert(int index,const T item){
    if(!ensureCapcity()){
        return false;
    }
    if(index < 1 || index > length){
        return false;
    }
    T *q = base + index - 1;
    T *p = base + length - 1;
    while( p >= q){
        *(p+1) = *p;
        p--;
    }
    *q = item;
    q = nullptr;
    p = nullptr;
    length ++;
    return true;
}

template<typename T>T Array<T>::del(int index){
    if(index<1 || index > length){
        return NULL;
    }
    T *q = base + index - 1;
    T item = *q;
    ++q;
    T *p = base + length;
    while(q <= p){
        *(q-1)=*q;
        ++q;
    }
    length --;
    return item;
}

template<typename T>T Array<T>::objectAt(int index){
    if(index<1 || index > length){
        return NULL;
    }
    T *q = base;
    return *(q + index - 1);
}

template <typename T>void Array<T>::display(){
    T *q = base;
    T *p = base +length - 1;
    while (q<=p) {
        cout << *(q++)<<" ";
    }
    cout << endl;
}

使用:

#include <iostream>
#include "Array.cpp"
using namespace std;

int main(int argc, const char * argv[]) {
    Array<int> array = *new Array<int>;
    array.init();
    array.add(1);
    array.insert(1,2);
    array.objectAt(1);
    return 0;
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • C、C++之動態(tài)數(shù)組的實現(xiàn) 本篇博客基于筆者本人正在學習的C++上機課程作業(yè),主要代碼由C語言構成。由于C語言沒有...
    largerthanlife閱讀 1,383評論 0 1
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,624評論 30 472
  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,428評論 6 13
  • 循環(huán)引用:http://ios.jobbole.com/82077/類別的作用功能:1.擴充現(xiàn)有類的功能2.對現(xiàn)有...
    得一切從簡閱讀 582評論 0 1
  • 1. “關于他想和你在一起這件事,你是怎么想的?”坐在我對面的閨蜜筱小細細地嘬了一點眼前的酒,用一種“果然如我所料...
    不覚閱讀 2,587評論 43 55

友情鏈接更多精彩內(nèi)容