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

2025-05-27 0 87

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

头文件: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++实现方法(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 动态数组C++实现方法(分享) https://www.kuaiidc.com/73497.html

相关文章

发表评论
暂无评论