-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferVector.h
More file actions
155 lines (122 loc) · 3.66 KB
/
referVector.h
File metadata and controls
155 lines (122 loc) · 3.66 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
#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__
#include <memory>
using namespace std;
template<class T>
class Vector {
public:
typedef T value_type;
typedef value_type *iterator; // vector的迭代器就是原生指针
typedef value_type *pointer;
typedef value_type &reference;
typedef size_t size_type;
public:
Vector() : start(NULL), finish(NULL), end_of_storage(NULL) {
}
Vector(size_type n, const value_type &value) {
start = alloc.allocate(n);
end_of_storage = finish = start + n;
uninitialized_fill_n(start, n, value);
}
Vector(size_type n) {
start = alloc.allocate(n);
end_of_storage = finish = start + n;
uninitialized_fill_n(start, n, value_type());
}
~Vector() {
// 顺序调用元素的析构函数
for (iterator i = start; i != finish; ++i)
alloc.destroy(i);
// 销毁分配的空间
if (start != NULL)
alloc.deallocate(start, end_of_storage - start);
}
iterator begin() const {
return start;
}
iterator end() const {
return finish;
}
size_type size() const {
return end() - begin(); // 使用接口函数,包裹性更好
}
size_type capacity() const {
return end_of_storage - begin(); // 使用接口函数,包裹性更好
}
bool empty() const {
return begin() == end();
}
// 返回的引用可被修改
reference front() {
return *(begin());
}
// 返回的引用可被修改
reference back() {
return *(end() - 1);
}
reference operator[](const size_type n) {
return *(begin() + n);
}
const reference operator[](const size_type n) const {
return *(begin() + n);
}
void push_back(const value_type &value) {
if (finish == end_of_storage)
reallocate(); // 存储空间已满,则重新分配内存
alloc.construct(finish, value);
++finish;
}
void reallocate();
void pop_back() {
--finish;
alloc.destroy(finish); // 析构最后一个函数,但不释放空间
}
// 清除一个元素
iterator erase(iterator position) {
if (position + 1 != finish)
copy(position + 1, finish, position);
--finish;
alloc.destroy(finish);
return position;
}
// 清除一段元素
iterator erase(iterator first, iterator last) {
if (first < start || last > finish)
throw std::invalid_argument("Invalid input.");
copy(last, finish, first);
int len = last - first;
while (len--)
alloc.destroy(--finish);
return first;
}
void clear() {
erase(begin(), end());
}
private:
iterator start;
iterator finish;
iterator end_of_storage;
private:
static std::allocator<value_type> alloc; // 空间配置器,采用静态属性节省空间
};
template<class Type>
std::allocator<Type> Vector<Type>::alloc;
template<class Type>
void Vector<Type>::reallocate() {
size_type oldsize = size();
size_type newsize = 2 * (oldsize == 0 ? 1 : oldsize);
// 分配新的内存空间
iterator newstart = alloc.allocate(newsize);
uninitialized_copy(start, finish, newstart);
// 顺序调用每个元素的析构函数
for (iterator i = start; i != finish; ++i)
alloc.destroy(i);
// 销毁分配的空间,销毁之前主要检查是否为NULL
if (start != NULL)
alloc.deallocate(start, end_of_storage - start);
// 更新下标
start = newstart;
finish = start + oldsize;
end_of_storage = start + newsize;
}
#endif