动态数组C++实现方法(分享)

所属分类: 软件编程 / C 语言 阅读数: 66
收藏 0 赞 0 分享

回顾大二的数据结构知识。从数组开始。实现了一个可自动扩充容量的泛型数组。

头文件: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;
}

以上这篇动态数组C++实现方法(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多