手写SGISTL vector

对标SGISTL版本为GUNC++2.91,实现其vector全部功能,使用分配器也是手写对标GUNC++2.91的allocator(暂时没写内存池的版本),使用方式using namespace Allocator;即可。纪念一下,留着以后改进用。

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
#include <iostream>
#include <stdlib.h>

namespace allocator {
using std::cout;
using std::cin;
using std::endl;
using std::cerr;

template <class T>
class Allocate{
public:
T* allocate(uint size){
return (T*)malloc(sizeof(T)*size);
}

void construct(T* pointer,const T& value){
new(pointer)T(value);//拷贝构造
}

void deallocate(T* pointer){
pointer->~T();
}

void destory(T* pointer){
free(pointer);
}

};

template <class T>
class vector{
private:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t distance;
typedef ptrdiff_t difference_type;

pointer _first;
pointer _last;
pointer _end;
Allocate<T> allocate;

void expand(uint size,uint resize = -1){
if(resize != -1){
//如果要变更的长度小于现有长度
if(resize < _end - _first) _last = _first + resize;
size = resize;
}
if(_first == nullptr){
_first = allocate.allocate(size);
_last = _first;
_end = _first + size;
}
else{
uint index = (uint)(_last - _first);//扩容
T* tmp = allocate.allocate(size);
for(int i = 0; i < index; i++) allocate.construct(tmp+i, _first[i]);
for(T* p = _first;p != _last;++p)allocate.deallocate(p);//析构
allocate.destory(_first);
_first = tmp;
_last = _first + index;
_last = _first + size;
}
}
public:
vector(uint size = 10){
expand(size);
}

~vector(){
for(T* p = _first; p != _last; ++p)allocate.deallocate(p);
allocate.destory(_first);
_first = _last = _end = nullptr;
}

vector<T>& operator = (const vector<T>& rhs){
if(this == &rhs)return *this;
for(pointer *p = _first; p != _last; ++p) allocate.deallocate(p);
allocate.destory(_first);
_first = allocate.allocate(rhs._end - rhs._first);
_last = rhs._last;
for(uint i = 0; i < _last - _first; i++)
allocate.construct(_first + i, *(rhs._first + i));
return this;
}

value_type& operator[](uint index){
if(index < _last - _first)return _first[index+1];
else{
cerr<<"Error: index > scope !\n";
exit(0);
}
}



bool empty(){
return _first == _last;
}

distance size(){
return _last - _first;
}

void push_back(const value_type& value){
++_last;
if(_last == _end)expand((uint)(_end - _first));
allocate.construct(_last, value);
}

void pop_back(){
if(_last == _first){
cerr<<"error: the vector is empty!\n";
exit(0);
}
--_last;
}

value_type back(){
return *_last;
}

value_type front(){
return *_first;
}

pointer begin(){
return _first;
}

pointer end(){
return _last+1;
}

pointer erase(pointer p){
int i = 0;
for(; p + i != _last; i++)
allocate.construct(p+i, *(p+i+1));
_last--;
return _last - i;
}

pointer insert(pointer pos,const value_type& value){
if(_last+1 == _end)expand((uint)(_end - _first));
_last++;
pointer p = _last;
for(; p != pos + 1; p--){
allocate.construct(p, *(p-1));
}
allocate.construct(p, value);
return pos;
}

void resize(uint size){
if(size > _end - _first)expand(size);
else expand(0,size);
}


};

};

using namespace::allocator;

class test{
public:
test(int i){
cout<<"构造函数"<<endl;
this->i = i;
}
~test(){
cout<<"析构函数"<<endl;
}
void print(){
cout<<"i = "<<i <<endl;
}
int i = 0;
};

int main(int argc, const char * argv[]) {
vector<int>v;
for(int i = 0; i < 10; i++) static_cast<void>(v.push_back(i)),cout<<v[i]<<"\t";cout<<endl;
v.erase(v.begin()+3);
v.insert(v.begin()+2,5);
for(int i = 0; i < v.size(); i++)cout<<v[i]<<"\t";cout<<endl;
return 0;
}