Vector的实现

Last updated on June 9, 2024 pm

所谓“向量”

绝大多数编程语言中均提供静态数组和动态数组的实现,C++中对动态数据的实现有std::Vector,即所谓向量。

动态数组顾名思义,是存储某类数据类型的一个容器,应提供对容器内数据增删改查的能力。

动态数组实现

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
149
150
151
152
153
154
#pragma once
template<class T>
class Vector {
public:
Vector();
Vector(int capaity);
~Vector();
public:
bool at(int index, T* pElement);
bool push_back(T& elemnt);
void pop_back();
bool insert(int index, T& elemnt);
int capaity();
void clear();
bool empty();
void earse(int index);
int size();
private:
bool expand();
bool isOverLen();
private:
int m_index;
int m_increment;
int m_len;
int m_initSize;
int m_eleSize;
T* m_pVector;
};

template <class T>
Vector<T>::Vector() : m_initSize(10), m_increment(5) {
this->m_pVector = new T[m_initSize];
m_len = m_initSize;
m_index = 0;
m_eleSize = sizeof(T);
}

template <class T>
Vector<T>::Vector(int capaity) : m_initSize(capaity), m_increment(5) {
this->m_pVector = new T[m_initSize];
m_len = capaity;
m_index = 0;
m_eleSize = sizeof(T);
}

template <class T>
Vector<T>::~Vector() {
delete[] this->m_pVector;
}

template <class T>
bool Vector<T>::isOverLen() {
return m_index >= m_len;
}

template <class T>
bool Vector<T>::at(int index, T* pElement) {
if (index >= m_index) {
return false;
}
*pElement = m_pVector[index];
return true;
}

template <class T>
bool Vector<T>::push_back(T& elemnt) {
if (this->isOverLen() && !this->expand()) {
return false;
}
m_pVector[m_index++] = elemnt;
return true;
}

template <class T>
void Vector<T>::pop_back() {
m_index--;
}

template <class T>
bool Vector<T>::insert(int index, T& elemnt) {
if (index > m_index) {
return false;
}

if (this->isOverLen() && !this->expand()) {
return false;
}

if (index == m_index) {
this->m_pVector[m_index++] = elemnt;
return true;
}

for (int i = m_index; i > index; i--) {
m_pVector[i] = m_pVector[i - 1];
}

m_pVector[index] = elemnt;
m_index++;
return true;
}

template <class T>
int Vector<T>::capaity() {
return this->m_initSize;
}

template <class T>
void Vector<T>::clear() {
delete[] m_pVector;
this->m_index = 0;
this->m_len = m_initSize;
this->m_pVector = new T[m_initSize];
}

template <class T>
bool Vector<T>::empty() {
return this->m_index == 0;
}

template <class T>
void Vector<T>::earse(int index) {
if (index >= m_index) {
return;
}
for (int i = index; i < m_index; i++) {
m_pVector[i] = m_pVector[i + 1];
}
m_index--;
}

template <class T>
int Vector<T>::size() {
return m_index;
}

template <class T>
bool Vector<T>::expand() {
int newLen = this->m_len + this->m_increment;
T* newArray = new T[newLen];

if (newArray == nullptr) {
return false;
}

for (int i = 0; i < m_len; i++) {
newArray[i] = m_pVector[i];
}
delete[] m_pVector;

m_pVector = newArray;
m_len = newLen;
return true;
}

Vector的实现
http://dubhehub.github.io/blogs/2024060523010046412.html
Author
Sin
Posted on
June 5, 2024
Licensed under