C++泛型动态数组实现

实现了一个可自动扩充容量的泛型数组。

头文件:Array.h

#ifndef Array_hpp
#define Array_hpp

template <class T>
class Array{
private:
    T *base;        //数组首地址
    int length;     //数组中元素
    int size;       //数组大小,以数组中元素的大小为单位
public:
    //初始化数组,分配内存
    bool init();
    //检查内存是否够用,不够用就增加
    bool ensureCapcity();
    //添加元素到数组尾
    bool add(T item);
    //插入元素到数组的具体位置,位置从1开始
    bool insert(int index,T item);
    //删除指定位置的元素并返回,位置从1开始
    T del(int index);
    //返回指定位置的元素
    T objectAt(int index);
    //打印数组所有元素
    void display();
};

#endif /* Array_hpp */

实现: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;
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • C、C++之动态数组的实现 本篇博客基于笔者本人正在学习的C++上机课程作业,主要代码由C语言构成。由于C语言没有...
    largerthanlife阅读 1,431评论 0赞 1
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,753评论 30赞 472
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,214评论 6赞 13
  • 循环引用:http://ios.jobbole.com/82077/类别的作用功能:1.扩充现有类的功能2.对现有...
    得一切从简阅读 604评论 0赞 1
  • 1. “关于他想和你在一起这件事,你是怎么想的?”坐在我对面的闺蜜筱小细细地嘬了一点眼前的酒,用一种“果然如我所料...
    不覚阅读 2,596评论 43赞 55

友情链接更多精彩内容