24 #ifndef mySTL_VECTOR_HPP
25 #define mySTL_VECTOR_HPP
27 #include "helpers.hpp"
28 #include "algorithm.hpp"
40 vector_base() : start_(0), finish_(0), end_of_storage_(0) {}
43 start_ = GetArrayMemory<T>(
n);
45 end_of_storage_ = start_ +
n;
50 FreeArrayMemory(start_);
55 swap(start_, that.start_);
56 swap(finish_, that.finish_);
57 swap(end_of_storage_, that.end_of_storage_);
67 typedef const T* const_iterator;
70 explicit vector(
size_t n) : vec_(n)
72 vec_.finish_ = uninit_fill_n(vec_.start_, n, T());
75 ~
vector() { destroy(vec_.start_, vec_.finish_); }
79 vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
83 size_t capacity()
const {
return vec_.end_of_storage_ - vec_.start_; }
85 size_t size()
const {
return vec_.finish_ - vec_.start_; }
87 T& operator[](
size_t idx) {
return *(vec_.start_ + idx); }
88 const T& operator[](
size_t idx)
const {
return *(vec_.start_ + idx); }
90 const T* begin()
const {
return vec_.start_; }
91 const T* end()
const {
return vec_.finish_; }
93 void push_back(
const T& v)
95 if (vec_.finish_ != vec_.end_of_storage_) {
96 construct(vec_.finish_, v);
100 vector tmp(size() * 2 + 1, *
this);
101 construct(tmp.vec_.finish_, v);
107 void resize(
size_t n,
const T& v)
109 if (n == size())
return;
112 T* first = vec_.start_ +
n;
113 destroy(first, vec_.finish_);
114 vec_.finish_ -= vec_.finish_ - first;
118 tmp.vec_.finish_ = uninit_fill_n(tmp.vec_.finish_, n - size(), v);
123 void reserve(
size_t n)
125 if (capacity() < n) {
133 vec_.Swap(that.vec_);
143 if (n > other.size())
144 vec_.finish_ = uninit_copy(other.vec_.start_, other.vec_.finish_,
153 #endif // mySTL_VECTOR_HPP