-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
57 lines (57 loc) · 1.23 KB
/
vector.cpp
File metadata and controls
57 lines (57 loc) · 1.23 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
template<class > class Array{
nint * ptr;
int maxSize;
int nowSize;
public:
Array(int _size=0){
maxSize = _size>0 ? _size: 256;
nowSize = 0;
ptr = new nint[maxSize];
}
~Array(){delete [] ptr;}
int size(){return nowSize;}
int back(){return ptr[nowSize-1];}
Array(Array & b){
maxSize = b.maxSize;
nowSize = b.nowSize;
//delete [] ptr;
ptr = new nint[maxSize];
for(int i=0; i<nowSize; i++)
ptr[i]=b[i];
}
void pb(nint x){
if(nowSize==maxSize){
nint * oldptr = ptr;
ptr = new nint[maxSize<<=1];
memcpy(ptr,oldptr,nowSize*sizeof(nint));
//for(int i=0; i<nowSize; i++)ptr[i]=oldptr[i];
//delete [] oldptr;
}
ptr[nowSize++]=x;
}
void pop(){
if(nowSize<maxSize>>2){
nint * oldptr = ptr;
ptr = new nint[maxSize>>=1];
for(int i=0; i<nowSize; i++)
ptr[i]=oldptr[i];
//delete [] oldptr;
}
nowSize--;
}
nint & operator[](int i){
if(i>=nowSize)
printf("Out Of Range\n"),exit(-1);
else
return ptr[i];
}
Array operator=(Array & b){
maxSize = b.maxSize;
nowSize = b.nowSize;
//delete [] ptr;
ptr = new nint[maxSize];
for(int i=0; i<nowSize; i++)
ptr[i]=b[i];
return b;
}
};