forked from MouCoder/cpp_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector
More file actions
152 lines (139 loc) · 2.28 KB
/
vector
File metadata and controls
152 lines (139 loc) · 2.28 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
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
namespace myvector
{
template<class T>
class vector
{
public:
typedef T* iterator;
iterator begin()
{
return start;
}
iterator end()
{
return finish;
}
//默认构造函数
vector()
:start(nullptr)
, finish(nullptr)
, end_of_storage(nullptr)
{}
//容量
size_t capacity()
{
return end_of_storage - start;
}
size_t size()
{
return finish - start;
}
void reserve(size_t n)
{
if (n > capacity())
{
//开新空间
T* tmp = new T[n];
//拷贝旧空间的内容
memcpy(tmp, start, sizeof(T)*size());
//改变容量
finish = tmp + size();
end_of_storage = tmp + n;
//释放旧空间
T* tmp1 = start;
start = tmp;
tmp = nullptr;
}
}
void resize(size_t n, const T& val = T())
{
//判断容量
if (n > capacity())
reserve(n);
//如果n<size
if (n < size())
{
finish = start + n;
}
else
{
while (finish != start + n)
{
*finish = val;
finish++;
}
}
}
//检查空间
void check_capacity()
{
if (finish == end_of_storage)
{
//如果当前不为空,就扩2倍,为空就开4个吧
size_t newcapacity = finish == nullptr ? 4 : capacity()*2;
reserve(newcapacity);
}
}
T& operator[](size_t pos)
{
assert(pos < size());
return start[pos];
}
//插入
void push_back(const T& x)
{
insert(finish,x);
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= start && pos <= finish);
size_t pos1 = pos - start;
check_capacity();
//解决迭代器失效
pos = start + pos1;
//移动数据
iterator end = finish;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
//插入数据
*pos = x;
finish++;
return pos;
}
//删除数据
void pop_back()
{
assert(finish > start);
finish--;
}
iterator erase(iterator pos)
{
assert(pos >= start && pos < finish);
iterator it = pos + 1;
while (it != finish)
{
*(it - 1) = *it;
++it;
}
--finish;
return pos;
}
//析构函数
~vector()
{
delete[] start;
start = finish = end_of_storage = nullptr;
}
private:
iterator start;
iterator finish;
iterator end_of_storage;
};
}