-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.cpp
More file actions
134 lines (114 loc) · 1.94 KB
/
Vector.cpp
File metadata and controls
134 lines (114 loc) · 1.94 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
#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 value_type& reference;
typedef size_t SizeType;
iterator start;
iterator finish;
iterator end_of_capacity;
Vector()
:start(0)
,finish(0)
,end_of_capacity(0)
{}
Vector(SizeType n ,const& T value = T())
:start(new T[n])
{
for(SizeType idx = 0;idx < n;idx++){
start[n] = value;
}
}
~Vector()
{
delete[] start;
}
iterator Begin()
{
return start;
}
iterator End()
{
return finish;
}
SizeType Size()const
{
return (SizeType)(End() - Begin());
}
SizeType Capacity()const
{
return (SizeType)(end_of_capacity - Begin());
}
bool empty()const
{
return Begin() == End();
}
reference front()
{
return *Begin();
}
reference back()
{
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;
}
iterator erase(iterator pos)
{
if(End() == pos +1){
delete pos;
--finish;
}
point pCur = pos;
point pNext = pos +1;
while(pNext != finish){
*pCur++ = *pNext++;
}
delete pCur;
--finish;
}
};
int main()
{
Vector<int> v1(5,6);
system("pause");
return 0;
}