-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvector.hpp
240 lines (240 loc) · 6.71 KB
/
myvector.hpp
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
#include<cstdio>
#include<cstdlib>
#include<cstddef>
#include<initializer_list>
#ifndef MY_VECTOR_WC
#define MY_VECTOR_WC
static const int defult_capacity = 4 ;
template <typename T >
class vector{
public:
typedef T* Pointer;
typedef T value_type;
typedef T& reference;
typedef size_t size_type;
typedef const T& const_reference;
typedef const T* const_iterator;
typedef const T* const_Pointer;
typedef ptrdiff_t difference_type;
private:
Pointer head;
size_type len;
size_type capcity;
public:
vector():len(0),capcity(defult_capacity),head(new value_type[defult_capacity]){}
vector(size_type n,value_type a):len(n),capcity(2*n){
head=new value_type[capcity];
for (int i = 0; i < n; ++i)
head[i]=a;
}
explicit vector(size_type n):len(n),capcity(2*n){
head=new value_type[capcity];
for (int i = 0; i < n; ++i)
head[i]=0;
}
vector(std::initializer_list<value_type> init){
len=init.size();
capcity=2*len;
head=new value_type[capcity];
int i=0;
for(auto n:init)
head[i++]=n;
}
~vector(){delete[] head;}
vector & operator= (const vector & other ){
this->head=other.head;
this->capcity=other.capcity;
this->len=other.len;
return *this;
}
value_type & operator[] (value_type i){return head[i];}
class iterator
{
private:
Pointer p;
public:
iterator (Pointer _p=NULL):p(_p){}
iterator operator ++(int){
Pointer p2=p;
++p;
return p2;
}
iterator operator ++(){return ++p;}
iterator operator --(int){
Pointer p2=p;
--p;
return p2;
}
iterator operator --(){return --p;}
iterator operator [](int i){return p+i;}
friend bool operator == (const iterator &lsh,const iterator &rsh){return lsh.p==rsh.p;}
friend bool operator != (const iterator &lsh,const iterator &rsh){return lsh.p!=rsh.p;}
value_type operator *(){return *p;}
value_type operator ->(){return &p;}
};
class reverse_iterator
{
private:
Pointer p;
public:
reverse_iterator (Pointer _p=NULL):p(_p){}
reverse_iterator operator ++(int){
Pointer p2=p;
--p;
return p2;
}
reverse_iterator operator ++(){return --p;}
reverse_iterator operator --(int){
Pointer p2=p;
++p;
return p2;
}
reverse_iterator operator --(){return ++p;}
reverse_iterator operator [](int i){return p+i;}
friend bool operator == (const reverse_iterator &lsh,const reverse_iterator &rsh){return lsh.p==rsh.p;}
friend bool operator != (const reverse_iterator &lsh,const reverse_iterator &rsh){return lsh.p!=rsh.p;}
value_type operator *(){return *p;}
};
public:
iterator begin() {return head;}
const_iterator begin()const {return head;}
const_iterator cbegin()const noexcept {return &head[len];}
iterator end() { return &head[len];}
const_iterator end()const {return &head[len]; }
const_iterator cend()const noexcept {return head;}
reverse_iterator rbegin() {return &head[len-1];}
const reverse_iterator rbegin()const {return &head[len-1];}
const reverse_iterator crbegin()const noexcept {return &head[len-1];}
reverse_iterator rend() {return head-1;}
const reverse_iterator rend()const {return head-1;}
const reverse_iterator crend()const noexcept {return head-1;}
reference at( size_type pos ){
if(pos<0||pos>len)
exit(1);
return head[pos];
};
size_type size() const { return len; }
size_type capacity() const { return capcity; }
bool empty() const { return size() == 0; }
reference back() const { return head[ len-1 ]; }
private:
void resize( size_type new_len ){
if( new_len > capcity )
reallocate( new_len*2 );
len = new_len;
}
void reallocate( size_type new_cap ){
if( new_cap < len ) return;
value_type *old = head;
head = new value_type[ new_cap ];
for( int k = 0; k < len; k++ )
head[k] = old[k];
capcity = new_cap;
delete [] old;
}
public:
void reserve(size_type new_cap)
{
if(len==0)
head= new Pointer[new_cap];
else
{
Pointer old=head;
size_type oldcap=capcity;
capcity=new_cap;
head= new Pointer[new_cap];
for (int i = 0; i < oldcap; ++i)
head[i]=old[i];
delete[] old;
}
}
void push_back(const value_type x){
if(len == capcity)
reallocate(2*capcity+1);
head[len++]=x;
}
void push_back(const iterator x){
if(len == capcity)
reallocate(2*capcity+1);
head[len++]=*x;
}
void pop_back() {
if(len==0)
exit(1);
len--;
}
void clear(){
while(len)
pop_back();
}
iterator insert( iterator it, const_reference value ){
if(len == capcity)
reallocate(2*capcity + 1);
for (auto i = end()-1; i != it; --i)
*i=*(i-1);
*it=value;
len++;
return it;
}
iterator insert( size_type pos, const_reference value ){
if(len == capcity)
reallocate(2*capcity + 1);
for (auto i = end()-1; i > pos; --i)
head[i]=head[i-1];
head[pos]=value;
len++;
return &head[pos];
}
iterator erase(size_type pos)
{
if(len==0)
exit(1);
for(int i=pos;i<len;i++)
head[i]=head[i+1];
len--;
return begin()+pos;
}
iterator erase( iterator it ){
if(len==0)
exit(1);
for (auto i = it; i != end(); ++i)
*i=*(i+1);
len--;
if(len)
return it;
else
return end();
}
void swap( vector& other ) {
Pointer temp=other.head;
size_type tlen=len;
size_type tcapacity=capcity;
other.head=head;
head=temp;
len=other.size();
capcity=other.capacity();
other.len=tlen;
other.capcity=tcapacity;
}
Pointer data()
{
return head;
}
void shrink_to_fit()
{
capcity=len+1;
Pointer old=head;
Pointer head=new value_type[capcity];
for(int i=0;i<len;i++)
head[i]=old[i];
head=old;
delete[] old;
}
void assign(size_type n,const_reference value )
{
len=0;
for (int i = 0; i < n; ++i)
push_back(value);
}
};
#endif