https://github.com/KyleAndKelly/miniSTL
实现一个简易版的Vector
- miniVector.h
1 |
|
miniVector_Impl.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
template<typename T>
void miniVector<T>::memoryExpand(const int elem){
T* newArr = new T[elem*capacity];
cout<<"DEBUG: create newArr success"<<endl;
memcpy(newArr,vectorArr,size*sizeof(T));
delete[] vectorArr;
vectorArr = newArr;
capacity = elem*capacity;
cout<<"DEBUG: capacity "<< capacity<<endl;
}
template<typename T>
miniVector<T>::miniVector(const int cap){
capacity = cap;
size = 0;
vectorArr = new T[capacity];
cout<<"LOG: capcity "<<capacity<<endl;
cout<<"LOG: size "<<size<<endl;
}
template<typename T>
miniVector<T>::miniVector(const int ini_size,T ini_data){
capacity = 20+ini_size;
size = ini_size;
vectorArr = new T[capacity];
// memset(vectorArr, ini_data,ini_size);
for(int i=0;i<ini_size;i++)
{
vectorArr[i] = ini_data;
}
cout<<"LOG: capcity "<<capacity<<endl;
cout<<"LOG: size "<<size<<endl;
}
template<typename T>
miniVector<T>::~miniVector(){
delete[] vectorArr;
vectorArr = NULL;
}
template<typename T>
void miniVector<T>::push_back(const T& num)
{
if(vectorArr == NULL)
{
capacity = 2;
size = 0;
vectorArr = new T[capacity];
}
if(size >= capacity)
{
memoryExpand(2);//2倍扩容
}
vectorArr[size] = num;
++size;
cout<<"DEBUG: size="<<size<<endl;
cout<<"DEBUG: vectorArr["<<size-1<<"]="<<vectorArr[size-1]<<endl;
cout<<"DEBUG: capacity"<<capacity<<endl;
return;
}
template<typename T>
void miniVector<T>::print_info() const
{
cout<<"LOG vector capacity: "<< capacity<<endl;
cout<<"LOG vector size: "<< size<<endl;
cout<<"LOG vector data: ";
for(auto i=0;i<size;i++){
cout<<vectorArr[i]<<" ";
}
cout<<endl;
}
template<typename T>
T& miniVector<T>::operator[](const int& index) const
{
if(vectorArr == NULL)
{
cout<<"Error: Memory Not Initial!"<<endl;
}
if( index >= size || index < 0)
{
cout<<"Error: Out of Ranger!"<<endl;
}
return vectorArr[index];
}
template<typename T>
void miniVector<T>::insert(const int& index,const T& data){
if(vectorArr == NULL)//如果还没有初始化
{
cout<<"Error: Vector is not Initial!"<<endl;
return;
}
if(index >= size-1){
cout<<"Error: The insert index should be from 0 to "<<size-1<<" "<<endl;
return;
}
if(size>=capacity){
memoryExpand(2);
}
for(int i = size-1;i>= index;i--)
{
vectorArr[i+1]=vectorArr[i];
}
vectorArr[index]= data;
++size;
cout<<"DEBUG: vectorArr["<<index<<"]="<<vectorArr[index]<<endl;
return;
}
template<typename T>
void miniVector<T>::pop_back()
{
if(vectorArr == NULL || size == 0)
{
cout<<"Error: Vector size is 0!"<<endl;
return;
}
--size;
cout<<"LOG: size "<< size<<endl;
cout<<"LOG: capacity "<<capacity<<endl;
}
template<typename T>
miniVector<T>::miniVector(const miniVector& vec){
//深拷贝
capacity = vec.capacity;
size = vec.size;
vectorArr = new T[capacity];
memcpy(vectorArr,vec.vectorArr,size*sizeof(T));
cout<<"LOG: now in miniVector(const miniVector& vec) "<<endl;
cout<<"LOG: size "<<size<<endl;
cout<<"LOG: capacity "<<capacity<<endl;
}
template<typename T>
miniVector<T>& miniVector<T>::operator=(const miniVector& vec){
//深拷贝
if(this == &vec) return *this;
capacity = vec.capacity;
size = vec.size;
delete[] vectorArr;
vectorArr = new T[capacity];
memcpy(vectorArr,vec.vectorArr,size*sizeof(T));
cout<<"LOG: now in miniVector operator= "<<endl;
cout<<"LOG: size "<<size<<endl;
cout<<"LOG: capacity "<<capacity<<endl;
return *this;
}
template<typename T>
void miniVector<T>::clear()
{
size=0;
}mainTest.cpp
1 |
|