iterator for vector(对于ector来说, iterator可以直接复用基类的base iterator, 因为迭代模式是随机访问的, 并且vector的内存布局是连续的, 所以vector的iterator可以复用base iterator)
iterator之后就是my_vector具体实现。

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
// =========== vector iterator ===========
template <typename T>
class vector_iterator : public mystl::iterator<random_access_iterator_tag, T> {

public:
using iterator_type = T*;
using iterator_category = random_access_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;

private:
pointer ptr_;

public:
vector_iterator() : ptr_(nullptr) {}

explicit vector_iterator(pointer ptr) : ptr_(ptr) {}

reference operator*() const {return *ptr_;}

pointer operator->() const {return ptr_;}

vector_iterator& operator++() {++ptr_;return *this;}

vector_iterator operator++(int) {
vector_iterator temp = *this;
++ptr_;
return temp;
}

vector_iterator& operator--() {--ptr_;return *this;}

vector_iterator operator--(int) {
vector_iterator temp = *this;
--ptr_;
return temp;
}

vector_iterator operator+(difference_type n) const {
return vector_iterator(ptr_ + n);
}

vector_iterator operator-(difference_type n) const {
return vector_iterator(ptr_ - n);
}

difference_type operator-(const vector_iterator& other) const {
return ptr_ - other.ptr_;
}

bool operator==(const vector_iterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const vector_iterator& other) const {
return ptr_ != other.ptr_;
}
bool operator<(const vector_iterator& other) const {
return ptr_ < other.ptr_;
}
bool operator>(const vector_iterator& other) const {
return ptr_ > other.ptr_;
}

pointer base() const { return ptr_;}

};

And my vector with my new vector iterator:

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef MY_VECTOR_H
#define MY_VECTOR_H

#include <stdexcept>
#include <memory>
#include "my_allocator.h"
#include "my_iterator.h"

namespace mystl {

template <class T, class Alloc = MyAllocator<T>>
class MyVector {
public:
using value_type = T;
using allocator_type = Alloc;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename allocator_type::value_type*;
using const_pointer = const typename allocator_type::value_type*;
using iterator = vector_iterator<value_type>;
using const_iterator = vector_iterator<const value_type>;
using reverse_iterator = mystl::reverse_iterator<iterator>;
using const_reverse_iterator = mystl::reverse_iterator<const_iterator>;

private:
allocator_type alloc_;
pointer start_;
pointer finish_;
pointer end_of_storage_;

public:
// ==== Constructors ====
MyVector() : start_(nullptr), finish_(nullptr), end_of_storage_(nullptr) {};

explicit MyVector(size_type n, const_reference value = value_type())
: start_(alloc_.allocate(n)),
finish_(start_),
end_of_storage_(start_ + n) {
std::uninitialized_fill_n(start_, n, value);
finish_ = end_of_storage_;
}

MyVector(std::initializer_list<value_type> ilist){
start_ = alloc_.allocate(ilist.size());
finish_ = start_;
end_of_storage_ = start_ + ilist.size();
for (const auto& item : ilist) {
alloc_.construct(finish_, item);
++finish_;
}
}

~MyVector() {
clear();
if (start_) {
alloc_.deallocate(start_, end_of_storage_ - start_);
}
}


// ===== Baisc Member Functions =====
size_type size() const noexcept {
return static_cast<size_type>(finish_ - start_);
}

size_type capacity() const noexcept {
return static_cast<size_type>(end_of_storage_ - start_);
}

bool empty() const noexcept {
return start_ == finish_;
}

reference operator[](size_type i) noexcept {
return start_[i];
}

const_reference operator[](size_type i) const noexcept {
return start_[i];
}

reference at(size_type i){
if (i >= size()) {
throw std::out_of_range("Index out of range");
}
return start_[i];
}


void push_back(const_reference value) {
if (finish_ == end_of_storage_) {
reallocate();
}
alloc_.construct(finish_, value);
++finish_;
}

void pop_back() {
if (empty()) {
throw std::out_of_range("Vector is empty");
}
--finish_;
alloc_.destroy(finish_);
}

void clear() noexcept {
while (finish_ != start_) {
--finish_;
alloc_.destroy(finish_);
}
}

void reserve(size_type new_cap) {
if(new_cap <= capacity()){
return;
}
reallocate_to(new_cap);
}

//resize the vector: change to logical size
void resize(size_type new_size, const_reference value = value_type()){
size_type old_size = size();

if(new_size < old_size){
//destroy extra elements
while(finish_ != start_ + new_size){
--finish_;
alloc_.destroy(finish_);
}
}else if(new_size > old_size){
if(new_size > capacity()){
reallocate_to(std::max(new_size, capacity() * 2));
}
//construct new elements
while(finish_ != start_ + new_size){
alloc_.construct(finish_, value);
++finish_;
}
}
}

void shrink_to_fit(){
if(capacity() == size()) return;
reallocate_to(size());
}


// ===== Iterator Support =====
iterator begin() noexcept {
return iterator(start_);
}

iterator end() noexcept {
return iterator(finish_);
}

const_iterator begin() const noexcept {
return const_iterator(start_);
}

const_iterator end() const noexcept {
return const_iterator(finish_);
}

reverse_iterator rbegin() noexcept {
return reverse_iterator(iterator(finish_));
}

reverse_iterator rend() noexcept {
return reverse_iterator(iterator(start_));
}

const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(const_iterator(finish_));
}

const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(const_iterator(start_));
}

private:
void reallocate(){

size_type old_size = size();
size_type old_capacity = capacity();
size_type new_capacity = old_capacity == 0 ? 1 : old_capacity * 2;

pointer new_start = alloc_.allocate(new_capacity);
pointer new_finish = new_start;

//move old data to new memory
for(pointer p = start_; p != finish_; ++p, ++new_finish){
alloc_.construct(new_finish, std::move(*p));
alloc_.destroy(p);
}

if(start_){
alloc_.deallocate(start_, old_capacity);
}


start_ = new_start;
finish_ = new_finish;
end_of_storage_ = start_ + new_capacity;
}

void reallocate_to(size_type new_cap){
size_type old_size = size();
pointer new_start = alloc_.allocate(new_cap);
pointer new_finish = new_start;

//move old data to new memory
for(pointer p = start_; p != finish_; ++p, ++new_finish){
alloc_.construct(new_finish, std::move(*p));
alloc_.destroy(p);
}

//free old memory
if(start_){
alloc_.deallocate(start_, old_size);
}

start_ = new_start;
finish_ = new_finish;
end_of_storage_ = start_ + new_cap;
}

};
}

#endif // MY_VECTOR_H