如何重载operator[] 及其相关细节
如何使用 const_cast<>( ) 和 static_cast<>( )
模板类 如何内部声明,外部定义友元函数
使用memset( )、memcpy_s( )
使用sizeof( )
禁用移动构造 和 移动赋值
1 #pragma once 2 #include3 using std::ostream; 4 using std::cout; 5 using std::endl; 6 7 template 8 class vector 9 { 10 public: 11 explicit vector(int size); 12 vector(const vector & v); 13 vector(vector &&) = delete; 14 vector(int size, T vlaue); 15 vector(T* es, int len); 16 vector& operator=(const vector & v); 17 vector& operator=(vector &&) = delete; 18 19 int size() const; 20 int capacity() const; 21 void reserve(int min_capacity); 22 void push_back(T e); 23 void pop_back(); 24 void erase(int pos); 25 void insert(T e, int pos); 26 27 const T& operator[](int) const; 28 T& operator[](int); 29 template 30 friend ostream& operator<<(ostream& os, const vector & v); 31 private: 32 T* vec; 33 int _size; 34 int _capacity; 35 }; 36 37 template 38 inline ostream& operator<<(ostream& os, const vector & v) 39 { 40 os << endl << "输出: " << endl; 41 for (int i = 0; i < v._size; i++) 42 { 43 os << v.vec[i] << " "; 44 } 45 return os; 46 } 47 48 template 49 inline vector ::vector(int size) : _size(size) 50 { 51 _capacity = size + size / 3; 52 this->vec = new T[_capacity]; 53 memset(this->vec, 0x00, sizeof(T) * _capacity); 54 } 55 56 template 57 inline vector ::vector(int size, T vlaue) 58 { 59 this->_capacity = size + size / 3; 60 this->_size = size; 61 this->vec = new T[_capacity]; 62 for (int i = 0; i < _size; i++) 63 { 64 this->vec[i] = vlaue; 65 } 66 } 67 68 template 69 inline vector ::vector(T* es, int len) 70 { 71 this->_size = len; 72 this->_capacity = len + len / 3; 73 this->vec = new T[_capacity]; 74 memcpy_s(this->vec, len * sizeof(T), es, len * sizeof(T)); 75 } 76 77 template 78 inline vector ::vector(const vector& v) 79 { 80 this->_capacity = v._capacity; 81 this->_size = v._size; 82 this->vec = new T[_capacity]; 83 memcpy_s(this->vec, sizeof(T) * _capacity, v.vec, sizeof(T) * _capacity); 84 } 85 86 template 87 inline int vector ::size() const 88 { 89 return this->_size; 90 } 91 92 template 93 inline int vector ::capacity() const 94 { 95 return this->_capacity; 96 } 97 98 template 99 inline vector & vector ::operator=(const vector & v)100 {101 if (this == &v)return *this;102 if (this->vec != nullptr)delete[] this->vec;103 this->_capacity = v._capacity;104 this->_size = v._size;105 this->vec = new T[_capacity];106 memcpy_s(this->vec, _capacity * sizeof(T), v.vec, v._capacity * sizeof(T));107 return *this;108 }109 110 template 111 inline void vector ::reserve(const int min_capacity)112 {113 if (min_capacity < this->_capacity) return;114 else115 {116 int* n_vec = new T[min_capacity];117 for (int i = 0; i < _size; i++)118 {119 n_vec[i] = vec[i];120 }121 delete[] vec;122 this->vec = n_vec;123 this->_capacity = min_capacity;124 }125 }126 127 template 128 inline void vector ::push_back(T e)129 {130 if (this->_size >= this->_capacity)131 {132 this->reserve(this->_size + this->_size / 3);133 }134 this->vec[_size++] = e;135 }136 137 template 138 inline void vector ::pop_back()139 {140 if (this->_size != 0)141 this->_size -= 1;142 }143 144 template 145 inline void vector ::erase(int pos)146 {147 if (pos < 0 || pos >= _size)return;148 else149 {150 for (int i = pos; i < _size - 1; i++)151 {152 this->vec[i] = this->vec[i + 1];153 }154 _size -= 1;155 }156 }157 158 template 159 inline void vector ::insert(T e, int pos)160 {161 if (pos < 0 || pos > _size - 1)return;162 if (this->_size >= this->_capacity)163 {164 this->reserve(this->_size + this->_size / 3);165 }166 for (int i = _size; i > pos; i++)167 {168 this->vec[i] = this->vec[i - 1];169 }170 this->vec[pos] = e;171 }172 173 template 174 inline const T& vector ::operator[](int i) const175 {176 if (i < 0 || i >= this->_size)177 {178 cout << "下标 i=" << i << " 越界 退出";179 exit(1);180 };181 return this->vec[i];182 }183 184 template 185 T& vector ::operator[](int pos)186 {187 // 这个写法的意思是 将指针this 转成 const 型指针 再对其*解引 188 //return const_cast ((*static_cast *>(this))[pos]);189 190 //这个是先对this 指针解引 再转成const 而且&直接引用该对象 191 return const_cast (static_cast &>(*this)[pos]);192 193 //这个写法不不可取;先对this指针 解引,再转成const时 并不是&,所以会重新创建一个新对象194 //return const_cast (static_cast >(*this)[pos]);195 }