-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVector.h
380 lines (324 loc) · 6.29 KB
/
Vector.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//模拟实现的SGI_STL中的Vector的部分功能
//无空间配置器
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <iostream>
using namespace std;
#include <assert.h>
template<class T>
class Vector
{
public:
typedef T& Ref;
typedef const T& ConstRef;
typedef T* Ptr;
typedef const T* ConstPtr;
typedef T* Iterator;
typedef const T* ConstIterator;
public:
Ref operator=(ConstRef v);
Vector(ConstRef v);
public:
/////////////////////////////////////
//construct
Vector()
:_start(NULL)
, _finish(NULL)
, _endOfStorage(NULL)
{}
//用大小为n的数组构造Vector
Vector(const T *a, size_t n)
{
_start = new T[n];
for (size_t i = 0; i < n; ++i)
_start[i] = a[i];
_finish = _start + n;
_endOfStorage = _finish;
}
Vector(const Vector<T>& v)
{
_Expand(v.Capacity());
for (size_t i = 0; i < v.Size(); ++i)
_start[i] = v[i];
_finish = _start + v.Size();
_endOfStorage = _start + v.Capacity();
}
Ref operator[](size_t n)
{
assert(n >= 0);
return _start[n];
}
ConstRef operator[](size_t n)const
{
assert(n >= 0);
return *(Begin()+n);
}
Ref Back()
{
return *(End()-1);
}
ConstRef Back()const
{
return *(End() - 1);
}
Ref Front()
{
return *Begin();
}
ConstRef Front()const
{
return *Begin();
}
///////////////////////////////////
//iterator
Iterator Begin()
{
return _start;
}
ConstIterator Begin()const
{
return _start;
}
Iterator End()
{
return _finish;
}
ConstIterator End()const
{
return _finish;
}
////////////////////////////////////
//vector<T, Alloc>& vector<T, Alloc>::operator=(const vector<T, Alloc>& x) {
// if (&x != this) {
// if (x.size() > capacity()) {
// iterator tmp = allocate_and_copy(x.end() - x.begin(),
// x.begin(), x.end());
// destroy(start, finish);
// deallocate();
// start = tmp;
// end_of_storage = start + (x.end() - x.begin());
// }
// else if (size() >= x.size()) {
// iterator i = copy(x.begin(), x.end(), begin());
// destroy(i, finish);
// }
// else {
// copy(x.begin(), x.begin() + size(), start);
// uninitialized_copy(x.begin() + size(), x.end(), finish);
// }
// finish = start + x.size();
// }
// return *this;
//}
bool operator==(const Vector<T>& v)
{
return _start == v._start && _finish == v._finish && _endOfStorage == v._endOfStorage;
}
bool operator!=(const Vector<T>& v)
{
return _start != v._start && _finish != v._finish && _endOfStorage != v._endOfStorage;
}
Vector<T>& operator=(const Vector<T>& v)
{
if (this != &v){
_Destroy();
Vector(v);
}
return *this;
}
////////////////////////////////////
//capacity
size_t Size()const
{
return (size_t)(End() - Begin());
}
size_t MaxSize()
{
return (size_t)(-1) / sizeof(T);
}
size_t Capacity()const
{
return (size_t)(_endOfStorage - Begin());
}
void Resize(size_t n, const T& value)
{
size_t size = Size();
_Expand(n);
for (size_t i = size; i < n; ++i)
_start[i] = value;
_finish = _start + n;
}
void Resize(size_t n)
{
Resize(n, T());
}
void Reverse(size_t n)
{
_Expand(n);
}
bool Empty()
{
return Begin() == End();
}
////////////////////////////////////
//modifiers
void PushBack(const T& value)
{
if (_finish == _endOfStorage){
size_t size = Size();
size_t newSize = (0 != size) ? (((MaxSize()-size)<size) ? 0 : size * 2) : 1;
_Expand(newSize);
}
*_finish = value;
++_finish;
}
void PopBack()
{
assert(_start != _finish);
--_finish;
}
void Swap(Vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endOfStorage, v._endOfStorage);
}
/////////////////////////////////////
//deconstruct
~Vector()
{
_Destroy();
}
protected:
//若n大于当前容量,则重新开辟一个大小为n的空间,并将数据拷贝过去,否则什么也不干
void _Expand(size_t n)
{
if (n > Capacity()){
T *temp = new T[n];
size_t size = Size();
for (size_t i = 0; i < size; ++i)
temp[i] = _start[i];
if(NULL != _start)
delete[] _start;
_start = temp;
_finish = _start + size;
_endOfStorage = _start + n;
}
}
void _Destroy()
{
if (_start){
delete[] _start;
_start = _finish = _endOfStorage = NULL;
}
}
protected:
Iterator _start;
Iterator _finish;
Iterator _endOfStorage;
};
#if 1
#include <iostream>
using namespace std;
#include <string>
void PrintVector(const Vector<string>& V)
{
Vector<string>::ConstIterator it = V.Begin();
for (; it != V.End(); ++it)
cout << *it << ' ';
cout << endl;
cout << "Size: " << V.Size() << endl;
cout << "Capacity: " << V.Capacity() << endl;
}
//#include "D:\Github\STL\Function.h"
void TestVector()
{
Vector<string> v;
//v.Resize(5, 10);
//v.Resize(10, 20);
v.PushBack("你好");
v.PushBack("2222");
v.PushBack("3333");
v.PushBack("4444");
PrintVector(v);
Vector<string> v2;
v2.PushBack("11");
v2.PushBack("22");
v2.PushBack("33");
v2.PushBack("44");
PrintVector(v2);
v2.Swap(v);
PrintVector(v);
PrintVector(v2);
//cout << v.Back() << endl;
//cout << v.Front() << endl;
//v.PushBack(2);
//v.PushBack(2);
//v.PushBack(2);
//v.PushBack(2);
//PrintVector(v);
//
//v.Resize(3);
//PrintVector(v);
//v.Resize(5, 10);
//PrintVector(v);
//v.Resize(20);
//PrintVector(v);
//int a[] = {1, 2, 3, 4, 5};
//Vector<int> v1(a, sizeof(a)/sizeof(a[0]));
//PrintVector(v1);
//v1.PopBack();
//v1.PopBack();
//PrintVector(v1);
//Vector<int> v2;
//v2.PushBack(1);
//PrintVector(v2);
//v2.PushBack(2);
//PrintVector(v2);
//v2.PushBack(3);
//PrintVector(v2);
//v2.PushBack(4);
//PrintVector(v2);
//v2.PushBack(5);
//PrintVector(v2);
//v2.PushBack(6);
//PrintVector(v2);
//cout << "MAxSize:" << v2.MaxSize() << endl;
//Vector<int> v3(5, 10);
//PrintVector(v3);
//Vector<int> v4(10);
//PrintVector(v4);
}
//void PrintVector(const vector<int>& v)
//{
// vector<int>::const_iterator it = v.begin();
// for (; it != v.end(); ++it)
// cout << *it << ' ';
// cout << endl;
// cout << "Size: " << v.size() << endl;
// cout << "Capacity: " << v.capacity() << endl;
//}
//
//void test_list()
//{
// //vector<int> v;
// //v.push_back(1);
// //v.push_back(2);
// //v.push_back(3);
// //v.push_back(4);
//
// //vector<int> v2(v);
// //cout << *(v + 1) << ' ';
//
// vector<string> v;
// v.push_back("1111");
// v.push_back("2222");
// v.push_back("3333");
// v.push_back("4444");
//
// vector<string> v2(v);
//
// v[2] = v2[1];
// }
#endif
#endif