-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVector.h
More file actions
158 lines (158 loc) · 2.54 KB
/
Vector.h
File metadata and controls
158 lines (158 loc) · 2.54 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
#include<iostream>
using namespace std;
template <typename T>
class Vector
{
private:
T* arr;
int size_;
int capacity_;
public:
typedef T* Iterator;
Vector();
Vector(Vector<T> &);
Vector(T*,T*);
Vector(int);
~Vector(){}
int size();
int capacity();
Iterator begin();
Iterator end();
T& operator[](int);
void push_back(T);
void mkspace(int,bool);
void pop_back();
bool empty();
T& front();
T& back();
void clear();
void erase(Iterator);
};
template <typename T>
Vector<T>::Vector()
{
size_=capacity_=0;
arr=0;
}
template <typename T>
Vector<T>::Vector(int n)
{
arr=new T[n];
for(int i=0;i<n;i++)
arr[i]=0;
size_=n;
capacity_=n;
}
template <typename T>
Vector<T>::Vector(Vector<T> &k)
{
size_=k.size_;
capacity_=k.capacity_;
arr=new T[size_];
for(int i=0;i<size_;i++)
arr[i]=k.arr[i];
}
template <typename T>
Vector<T>::Vector(T *st,T *en)
{
size_=capacity_=en-st;
arr=new T[size_];
int i=0;
T *a=st;
while(a!=en)
{
arr[i++]=*a;
a++;
}
}
template <typename T>
int Vector<T>::size()
{
return size_;
}
template <typename T>
int Vector<T>::capacity()
{
return capacity_;
}
template <typename T>
typename Vector<T>::Iterator Vector<T>::begin()
{
return arr;
}
template <typename T>
typename Vector<T>::Iterator Vector<T>::end()
{
return arr+size_;
}
template <typename T>
T& Vector<T>::operator[](int i)
{
return arr[i];
}
template <typename T>
void Vector<T>::push_back(T data)
{
if(size_==capacity_)
{
if(capacity_==0)
mkspace(1,false);
else
mkspace(2*capacity_,true);
}
arr[size_]=data;
size_++;
}
template <typename T>
void Vector<T>::mkspace(int n,bool sig)
{
T* newarr;
int i;
newarr=new T[n];
if(newarr==NULL)
cout<<"\nMemory allocation error";
if(sig)
{
for(int i=0;i<size_;i++)
newarr[i]=arr[i];
}
if(arr!=NULL)
delete []arr;
arr=newarr;
capacity_=n;
}
template <typename T>
void Vector<T>::pop_back()
{
size_--;
}
template <typename T>
bool Vector<T>::empty()
{
return size_==0;
}
template <typename T>
T& Vector<T>::front()
{
return arr[0];
}
template <typename T>
T& Vector<T>::back()
{
return arr[size_-1];
}
template <typename T>
void Vector<T>::clear()
{
size_=capacity_=0;
arr=NULL;
}
template <typename T>
void Vector<T>::erase(Iterator it)
{
T *i=it,*j=it;
++i;
for(;i!=end();++i,++j)
*j=*i;
size_--;
}