实现 Vector 类

本文最后更新于:2021年3月4日 中午

实现 vector 类

参考《后台开发 核心技术与应用实践》p90

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <algorithm>
#include <iostream>
#include <assert.h>
using namespace std;


template<typename T>
class myVector {
private:

public:
/*默认构造函数*/
myVector() : array(0), theSize(0), theCapacity(0) {}
/*构造函数*/
myVector(const T& t, unsigned int n) : array(0), theSize(0), theCapacity(0) {
while (n--) {
push_back(t);
}
}
/*拷贝构造函数*/
myVector(const myVector<T>& other) : array(0), theSize(0), theCapacity(0) {
*this = other;
}

/*赋值函数*/
myVector<T>& operator = (myVector<T>& other) {
if (this == &other)
return *this;
clear();
theSize = other.size();
theCapacity = other.capacity();
array = new T[theCapacity];
for (unsigned int i = 0; i < theSize; ++i) {
array[i] = other[i];
}
return *this;
}

/*析构函数*/
~myVector() {
clear();
}

/* operator[] 函数*/
T& operator[] (unsigned int pos) {
assert(pos >= 0 && pos < theSize);
return array[pos];
}

/*element theSize*/
unsigned int size() {
return theSize;
}

/* alloc theCapacity */
unsigned int capacity() {
return theCapacity;
}

/* is empty */
bool empty() {
return theSize == 0;
}

/* clear myVector */
void clear() {
deallocator(array);
array = 0;
theSize = 0;
theCapacity = 0;
}

/* adds an element in the back of myVector */
void push_back(const T& t) {
insert_after(theSize - 1, t);
}

/* adds an element in the front of myVector */
void push_front(const T& t) {
insert_before(0, t);
}

/* inserts an element after the pos */
// the pos must be in [0, theSize]
void insert_after(int pos, const T& t) {
insert_before(pos + 1, t);
}

/* inserts an element before the pos */
// the pos must be less than myVector.size()
void insert_before(int pos, const T& t) {
if (theSize == theCapacity) {
resize(2 * theCapacity);
}
for (int i = (int)theSize - 1; i >= pos; --i) {
array[i+1] = array[i];
}
array[pos] = t;
theSize++;
}

/* erase an element in the pos*/
// pos must be in [0, theSize]
void erase(unsigned int pos) {
if (pos < theSize) {
for (unsigned int = pos+1; i < theSize; ++i) {
array[i-1] = array[i];
}
--theSize;
}
if (theSize == theCapacity / 4 && theCapacity / 2 != 0) {
resize(theCapacity / 2);
}
}


/* 动态扩缩容 */
void resize(int newCapacity) {
T* newArray = new T[newCapacity];
for (int i = 0; i < theSize; ++i) {
newArray[i] = array[i];
}
array = newArray;
theCapacity = newCapacity;
newArray = nullptr;
delete [] newArray;
}


private:
/* 分配内存 */
T* allocator(unsigned int size) {
return new T[size];
}

/* 释放内存 */
void deallocator(T* arr) {
if (arr) {
delete [] arr;
}
}


private:
T* array;
unsigned int theSize;
unsigned int theCapacity;
};

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!