-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyVector.cpp
More file actions
182 lines (154 loc) · 2.75 KB
/
MyVector.cpp
File metadata and controls
182 lines (154 loc) · 2.75 KB
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
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<memory.h>
using namespace std;
template <class T>
class Vector
{
public:
typedef T value_type;
typedef value_type* point;
typedef value_type* iterator;
typedef const value_type* Constiterator;
typedef value_type& reference;
typedef const value_type& Constreference;
typedef size_t SizeType;
typedef const size_t ConstSizeType;
Vector()
:start(0)
,finish(0)
,end_of_capacity(0)
{}
Vector(SizeType n ,const T& value = T())
:start(new T[n])
, finish(start)
, end_of_capacity(start)
{
for(SizeType idx = 0;idx < n;idx++){
start[idx] = value;
finish++;
}
end_of_capacity = start + n - 1;
}
~Vector()
{
delete[] start;
start = NULL;
}
iterator Begin()
{
return start;
}
iterator End()
{
return finish;
}
Constiterator Begin()const
{
return start;
}
Constiterator End()const
{
return finish;
}
SizeType Size()const
{
return (SizeType)(End() - Begin());
}
ConstSizeType Capacity()const
{
return (SizeType)(end_of_capacity - Begin());
}
bool empty()const
{
return Begin() == End();
}
reference front()
{
return *Begin();
}
reference back()
{
return *(End() - 1);
}
Constreference front()const
{
return *Begin();
}
Constreference back()const
{
return *(End() - 1);
}
void CheckCapacity()
{
if(End() == end_of_capacity){
T* pTemp = new T[Size()*2 + 1];
memcpy(pTemp,start,sizeof(T)*Size());
delete[] start;
start = pTemp;
finish = start + Size();
end_of_capacity = start + Size()*2 +1;
}
}
void PushBack (const T& x)
{
CheckCapacity();
*finish = x;
++finish;
}
void Insert(iterator pos,const T& x)
{
CheckCapacity();
if (End() == pos){
*pos = x;
++finish;
}
point pCur = finish - 1;
point pNext = finish;
while(pCur-- == pos){
*pNext-- = *pCur;
}
++finish;
*pos = x;
}
void erase(iterator pos)
{
if(End() == pos +1){
delete pos;
--finish;
}
point pCur = pos;
point pNext = pos +1;
while(pNext != finish){
*pCur++ = *pNext++;
}
--finish;
}
reference operator[] (SizeType n)
{
return *(Begin() + n);
}
protected:
iterator start;
iterator finish;
iterator end_of_capacity;
};
int main()
{
Vector<int> v1(10,5);
v1.PushBack(3);
v1.PushBack(4);
v1.PushBack(6);
//v1.Insert()
cout << v1[9] << endl;
cout << v1[10] << endl;
cout << v1[11] << endl;
cout << v1[12] << endl;
v1.Insert(&v1[3], 7);
cout << v1[3] << endl;
v1.erase(&v1[3]);
cout << v1[3] << endl;
system("pause");
return 0;
}