#include <iostream> using namespace std; template <typename T> class Myvector { private: T * first; T * last; T * end; public: //构造函数 Myvector(int size = 10) { this->first = new T[size]; this->last = this->first; this->end = this->first + size; } //析构函数 ~Myvector() { delete []first; } //拷贝构造 Myvector(const Myvector& other) { int size = other.end - other.first; this->first = new T[size]; int len = other.last - other.first; memmove(this->first,other.first,len*sizeof(T)); this->last = this->first + len; this->end = this->first + size; return *this; } //拷贝赋值 Myvector& operator = (const Myvector &other) { int size = other.end - other.first; if(this->first != nullptr) { delete []this->first; this->first = new T[size]; }else { this->first = new T[size]; } int len = other.last - other.first; memmove(this->first,other.first,len*sizeof(T)); this->last = this->first +len; this->end= this->first + size; return *this; } //T &at(int index) T &at(int index) { for(int i=*first;i<*last;i++) { if(first[i]=index) return first[i]; } } //empty() bool empty() { return this->last==this->first; } //full() bool full() { return this->end == this->last; } //front() T& front() { return *this->first; } //back() T& back() { return *this->last; } //size() int size() { return this->last-this->first; } //clear() void clear() { delete []first; this->first = nullptr; } //expand() 二倍扩容函数 void expand() { int size = this->last-this->first; T*temp = new T[2*size]; memcpy(this->temp,this->first,sizeof(T)*(this->end - this->first)); delete []first; this->first = this->temp; this->last = this->first + size; this->end = this->first + 2*size; return *this; } //push_back() void push_back(const T& value) { if(full()) { expand(); } *last++ = value; } //pop_back() void pop_back() { if(empty()) { return; } *last--; } }; int main() { cout << "Hello World!" << endl; return 0; }
思维导图: