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
| #include <initializer_list> #include <assert.h>
template <typename T> class Vector { public: typedef T* iterator; typedef const T* const_iterator;
Vector(); Vector(size_t n, const T& value = T()); Vector(const Vector<T>& v); Vector(Vector<T>&& v); Vector(const std::initializer_list<T>& lst); ~Vector(); Vector<T>& operator=(Vector<T> v);
iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const;
size_t size() const; void resize(size_t n, const T& val = T()); size_t capacity() const; bool empty() const; void reserve(size_t n);
T& operator[](size_t pos); const T& operator[](size_t pos) const;
void push_back(const T& val); void pop_back(); iterator insert(iterator pos, const T& val); iterator erase(iterator pos); void swap(Vector<T>& v); void clear(); private: T* _start = nullptr; T* _finish = nullptr; T* _endOfStorage = nullptr; };
template <typename T> Vector<T>::Vector() : _start(nullptr) , _finish(nullptr) , _endOfStorage(nullptr) {}
template <typename T> Vector<T>::Vector(size_t n, const T& value) : Vector() { reserve(n); while (n--) { push_back(value); } }
template <typename T> Vector<T>::Vector(const Vector<T>& v) : _start(new T[v.capacity()]) , _finish(_start + v.size()) , _endOfStorage(_start + v.capacity()) { for (size_t i = 0; i < v.size(); i++) { _start[i] = v[i]; } }
template<typename T> Vector<T>::Vector(Vector<T>&& v) : Vector() { swap(v); }
template<typename T> Vector<T>::Vector(const std::initializer_list<T>& lst) : _start(new T[lst.size()]) , _finish(_start) , _endOfStorage(_start + lst.size()) { for (const auto& e : lst) { *_finish = e; ++_finish; } }
template <typename T> Vector<T>::~Vector() { if (_start) { delete[] _start; _start = _finish = _endOfStorage = nullptr; } }
template <typename T> Vector<T>& Vector<T>::operator=(Vector<T> v) { swap(v); return *this; }
template <typename T> typename Vector<T>::iterator Vector<T>::begin() { return _start; }
template <typename T> typename Vector<T>::const_iterator Vector<T>::begin() const { return _start; }
template <typename T> typename Vector<T>::iterator Vector<T>::end() { return _finish; }
template <typename T> typename Vector<T>::const_iterator Vector<T>::end() const { return _finish; }
template <typename T> size_t Vector<T>::size() const { return _finish - _start; }
template <typename T> void Vector<T>::resize(size_t n, const T& val) { if (n <= size()) { _finish = _start + n; } else { if (n > capacity()) { reserve(n); } while (_finish < _start + n) { *_finish++ = val; } } }
template <typename T> size_t Vector<T>::capacity() const { return _endOfStorage - _start; }
template<typename T> bool Vector<T>::empty() const { return size() == 0; }
template <typename T> void Vector<T>::reserve(size_t n) { size_t size = this->size(); if (n > capacity()) { T* tmp = new T[n]; if (_start) { for (size_t i = 0; i < this->size(); i++) { tmp[i] = _start[i]; } } _start = tmp; _finish = _start + size; _endOfStorage = _start + n; } }
template <typename T> T& Vector<T>::operator[](size_t pos) { assert(pos < size()); return _start[pos]; }
template <typename T> const T& Vector<T>::operator[](size_t pos) const { assert(pos < size()); return _start[pos]; }
template <typename T> void Vector<T>::push_back(const T& val) { insert(end(), val); }
template <typename T> void Vector<T>::pop_back() { assert(!empty()); erase(end() - 1); }
template <typename T> typename Vector<T>::iterator Vector<T>::insert(iterator pos, const T& val) { assert(pos >= begin() && pos <= end()); size_t len = pos - _start; if (_finish == _endOfStorage) { size_t newC = capacity() == 0 ? 1 : 2 * capacity(); reserve(newC); } pos = _start + len; iterator end = _finish; while (end > pos) { *end = *(end - 1); --end; } *pos = val; ++_finish; return pos; }
template <typename T> typename Vector<T>::iterator Vector<T>::erase(iterator pos) { assert(pos < _finish && pos >= _start); iterator begin = pos + 1; while (begin < _finish) { *(begin - 1) = *begin; ++begin; } --_finish; return pos; }
template <typename T> void Vector<T>::swap(Vector<T>& v) { std::swap(_start, v._start); std::swap(_finish, v._finish); std::swap(_endOfStorage, v._endOfStorage); }
template<typename T> void Vector<T>::clear() { _finish = _start; }
|