-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
70 lines (69 loc) · 1.33 KB
/
Vector.h
File metadata and controls
70 lines (69 loc) · 1.33 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
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
#include"Alloctor.h"
#define INIL_SIZE 5
template<class T>
class Vector{
public:
Vector();
T get(int index);
void set(int ,const T&);
void push_back(const T&);
void remove_back();
inline int size(){ return size_num;}
void clear();
~ Vector();
// private :
int size_num;
int total_size;
T* start;
Alloctor<T> alloc;
void extend();
};
template<class T>
Vector<T>::Vector(){
alloc=Alloctor<T>();
size_num=0;
total_size=INIL_SIZE;
start=alloc.allocate(INIL_SIZE);
}
template<class T>
T Vector<T>::get(int index){
if(index>=size()) return T{};
return *(start+index);
}
template<class T>
void Vector<T>::set(int index,const T& toSet){
if(index>=size()) return;
*(start+index)=T{toSet};
}
template<class T>
void Vector<T>::push_back(const T& toAdd){
if(size_num== total_size) extend();
*(start+size())=T{toAdd};
++size_num;
}
template<class T>
void Vector<T>::remove_back(){
if(size_num==0) return;
--size_num;
}
template<class T>
void Vector<T>::clear(){
size_num=0;
}
template<class T>
void Vector<T>::extend(){
T* new_start=alloc.allocate(2*total_size);
for(int i=0;i<total_size;++i)
*(new_start+i)=*(start+i);
total_size*=2;
alloc.deallocate(start,size());
start=new_start;
}
template<class T>
Vector<T>::~Vector(){
alloc.deallocate(start,size());
}
#endif