服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 动态数组C++实现方法(分享)

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

2021-05-12 16:29jingxian C/C++

下面小编就为大家带来一篇动态数组C++实现方法(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

头文件:Array.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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;
}

使用:

?
1
2
3
4
5
6
7
8
9
10
11
12
#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++实现方法(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐