1 #pragma warning(disable:4996) 2 #include3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 using namespace std; 15 //template class alloc 16 //{public: 17 // alloc():a(),s(){ //成员变量默认初始化 18 // } 19 // t* allocate(size_t n){ 20 // return a.allocate(n); 21 // } 22 ////变长模板参数,allocator的第二个参数是变长的,要调用它,外层的construct也要有变长参数 23 //template void construct(t* p, const Args&...rest){ 24 // a.construct(p,rest... ); 25 // } 26 // void deallocate(t* p, size_t n) 27 // { 28 // a.deallocate(p, n); 29 // } 30 // void destory(t* p) 31 // { 32 // a.destory(p); 33 // } 34 // 35 // 36 //private: 37 // allocator a; 38 // string s; 39 // 40 //}; 41 //vector为空,capacity=0,否则预分配空间为16,当空间不足时,扩大两倍增长, 42 template > 43 class Vector{ 44 private: 45 T* start; 46 T* finish; 47 T* end_of_storage; 48 size_t length; 49 size_t capacity; 50 public: 51 typedef T* iterator; 52 typedef const T* const_iterator; 53 Vector():start(NULL),finish(0){ 54 end_of_storage = NULL; 55 length = 0; 56 capacity = 0; 57 } 58 Vector(const std::initializer_list &lst) 59 { 60 for (auto it = lst.begin(); it != lst.end();it++) 61 { 62 this->push_back(*it); 63 } 64 65 } 66 Vector(size_t n, const T&value = T()){ 67 Allocator a; 68 T*p = NULL; 69 if (n <= 16){ p = a.allocate(16); 70 capacity = 16; 71 } 72 else { 73 p = a.allocate(static_cast (1.5 * n)); 74 capacity = static_cast (1.5*n); 75 } 76 start = p; 77 finish=uninitialized_fill_n(p, n, value); 78 if (n <= 16) end_of_storage = start + 15; 79 else end_of_storage = p + static_cast (1.5*n)-1; 80 length = n; 81 } 82 /*Vector(size_t n, const T&value){ Vector(n, value); }*/ 83 Vector(const Vector& rhs){ 84 capacity = rhs.capacity; 85 length = rhs.length; 86 Allocator a; 87 start=a.allocate(capacity); 88 end_of_storage = start + capacity - 1; 89 finish = start + length ; 90 91 for (T* p = start,*q = rhs.start; p != finish; p++, q++){ 92 a.construct(p, *q); 93 } 94 } 95 void push_back(const T& value){ 96 if (capacity == 0){ //为空容器时需要申请新空间 97 Allocator a; 98 start = a.allocate(16); 99 a.construct(start, value);100 finish = start + 1;101 end_of_storage = start + 15;102 length = 1;103 capacity = 16;104 }105 else if (length < capacity){106 Allocator a;107 a.construct(finish++, value);108 length += 1;109 }110 else { //空间不足时1.5倍扩大空间111 Allocator a;112 auto old_capacity = capacity;113 auto new_start = a.allocate(static_cast (1.5*capacity));114 auto new_finish = uninitialized_copy(this->begin(), this->begin()+length, new_start); //没有使用迭代器形式115 new_finish = uninitialized_fill_n(new_finish, 1, value);116 //释放掉旧空间117 capacity = static_cast (1.5*capacity);118 length += 1;119 120 auto temp = start;121 while (temp != finish)122 {123 a.destroy(temp++);124 }125 a.deallocate(start, old_capacity);126 start = new_start;127 finish = new_finish;128 end_of_storage = start + capacity - 1;129 }130 }131 void pop_back(){ //弹出的意义是不在含有该对象132 Allocator a;133 a.destroy(--finish); //finish指向尾后位置,自减后销毁了对象,不存在了134 length -= 1;135 136 }137 iterator insert(const_iterator position, const T&value);138 T& operator[](size_t n){ return *(start + n); } //如果index out of bound or Vector为空 ,让用户自己去处理139 const T& operator[](size_t n)const { return *(start + n); }140 T& front(){ return *start; }141 const T&front()const{ return *start; }142 T& back(){ return *(finish-1); }143 const T&back()const{ return *(finish - 1); }144 size_t size(){ return length; }145 size_t volume(){ return capacity; }146 iterator begin(){ return start; }147 const_iterator cbegin()const { return start; }148 iterator end(){ return finish; }149 const_iterator cend(){ return finish; }150 Vector& operator=(const Vector&rhs){151 if (this->start ==rhs.start) return *this; //两个相等,两个相同152 //释放掉原有的内存空间,allocator分配的空间如果不deallocate,会造成内存泄漏153 auto old_start = this->start;154 auto old_finish = this->finish;155 size_t old_capacity = this->capacity;156 capacity = rhs.capacity;157 length = rhs.length;158 Allocator a;159 start = a.allocate(capacity);160 end_of_storage = start + capacity - 1;161 finish = start + length;162 163 for (T* p = start, *q = rhs.start; p != finish; p++, q++){164 a.construct(p, *q);165 }166 Allocator b;//是需要重新创建b,还是用前面的a就可以完成下面的动作167 T* p = old_start;168 while (p!=old_finish)169 {170 b.destroy(p++);171 }172 b.deallocate(old_start, old_capacity);173 return *this;174 175 };176 ~Vector(){177 Allocator a;178 T* p = start;179 while (p!= finish)180 {181 a.destroy(p++);182 }183 a.deallocate(start, capacity);184 start = finish = end_of_storage = NULL; //是否多余语句185 cout << " dectr completed";186 }187 188 };189 template 190 class demo{191 public:192 demo():d(){}193 T size();194 private:195 T d;196 };197 class foo{198 public:199 foo() :a(5), s("dka"){}200 private:201 int a; string s;202 203 };204 205 int main()206 {207 Vector v1;208 cout << v1.volume() << " " << v1.size() << "\n";209 Vector v2(10);//10个空串,调用了string的default ctr210 cout << v2.size() << " " << v2.volume() << endl;211 cout << "the first item of v2: " << v2[0] << " or " << v2.front() << endl;212 //Vector v3(10, 'c'); no instance of parameters (int,char)213 Vector v4(10, "c");214 Vector v5(v4);215 cout << v5.back() << " " << v5.size() << v5.volume() << endl;216 v5 = v1;217 for (auto it = v5.begin(); it!= v5.end();it++)218 {219 cout << *it;220 }221 /*cout << v5[0] << endl;*/ 222 223 Vector v6(v1); //用空vector初始化另一个vector ;这两个vector都应该表现正常224 for (auto it = v6.cbegin(); it != v6.cend();it++)225 { 226 cout << *it << endl;227 }228 v1 = v4;//v1原本为空,用含10个元素的v4赋值,并用push_back添加尾元素229 for (auto x : v1) cout << x << endl;230 v1.push_back("hdwa");231 cout << v1.size() << v1[10] << endl;232 v1.pop_back();233 cout << v1.back() << endl;234 string s;235 Vector iv(10);236 for (auto y:iv)237 {238 printf("%d", y);239 }240 Vector v7={ "dak", "dj" };241 cout << v7.volume() << " " << v7.size() << v7[0] << " " << v7[1] << endl;242 243 244 }