简单的 vector
1 #pragma once 2 #include <memory.h> 3 #include <stdlib.h> 4 #include <iostream> 5 using std::ostream; 6 using std::cout; 7 using std::endl; 8 class vector{ 9 public : 10 vector(); 11 ~vector(); 12 explicit vector(int size_); 13 vector(int size_,int e); 14 vector(const vector&a); 15 vector(int *data,int len); 16 vector& operator=(const vector&a); 17 vector(vector&&)=delete; 18 vector& operator=(vector&&)=delete; 19 20 21 int size()const; 22 int capacity()const; 23 void reserve(int capacity_); 24 void resize(int size_); 25 int& operator[](int); 26 void insert(int pos,int x);//insert a new element before the position pos 27 void erase(int pos);//erase the element at the position pos; 28 void push_back(int x);//insert a new element at the end 29 void pop_back();//erase the last element 30 int& front();//return the first element 31 const int& front()const; 32 int& back();//return the last element 33 const int& back()const; 34 bool empty()const;//true if the size is 0 35 friend ostream& operator <<(ostream&os,const vector& a){ 36 for(int i=0;i<a._size;i++){ 37 cout<<a.elems[i]<<" "; 38 } 39 cout<<endl; 40 return os; 41 } 42 43 private: 44 int *elems; 45 int _size; 46 int _capacity; 47 bool _safePos(int a)const{ 48 if(a<0||a>_size-1)return false; 49 else return true; 50 } 51 }; 52 vector::vector(){ 53 _size=0; 54 _capacity=0; 55 elems=nullptr; 56 } 57 vector::~vector(){ 58 delete[] elems; 59 } 60 61 vector::vector(int size_):_size(size_),_capacity(size_){ 62 this->elems=new int[_capacity]; 63 memset(this->elems,0x3f,_capacity*4); 64 } 65 vector::vector(int size_, int e):_size(size_),_capacity(size_){ 66 this->elems=new int[_capacity]; 67 for(int i=0;i<size_;i++){ 68 this->elems[i]=e; 69 } 70 } 71 72 vector::vector(const vector &a){ 73 this->_size=a._size; 74 this->_capacity=a._capacity; 75 this->elems=new int[_capacity]; 76 memcpy_s(this->elems,_capacity*4,a.elems,_capacity*4); 77 } 78 vector::vector(int *data, int len){ 79 this->_capacity=len+len/3; 80 this->_size=len; 81 this->elems=new int[_capacity]; 82 memset(this->elems,0x3f,_capacity*4); 83 memcpy_s(this->elems,_size*4,data,len*4); 84 } 85 vector& vector::operator =(const vector&a){ 86 if(*this==a) return *this; 87 delete[] this->elems; 88 this->_size=a._size; 89 this->_capacity=a._capacity; 90 this->elems=new int[_capacity]; 91 memcpy_s(this->elems,_capacity*4,a.elems,_capacity*4); 92 return *this; 93 } 94 int vector::size()const{ 95 return this->_size; 96 } 97 int vector::capacity()const{ 98 return this->_capacity; 99 } 100 void vector::reserve(int capacity_){ 101 if(capacity_<=this->_capacity)return; 102 else{ 103 this->_capacity=capacity_; 104 int *es=new int[_capacity]; 105 memset(es,0x3f,_capacity*4); 106 memcpy_s(es,_size*4,elems,_size*4); 107 delete[] elems; 108 this->elems=es; 109 } 110 } 111 void vector::resize(int size_){ 112 if(size_<this->_size)this->_size=size_; 113 else if(size_<=this->_capacity){ 114 while(this->_size<size_){ 115 this->elems[this->_size++]=-1; 116 } 117 }else if(size_>this->_capacity){ 118 this->reserve(size_); 119 while(this->_size<this->_capacity){ 120 this->elems[this->_size++]=-1; 121 } 122 } 123 } 124 125 int& vector::operator [](int p){ 126 if(_safePos(p)) 127 return this->elems[p]; 128 exit(1); 129 } 130 void vector::insert(int pos, int x){ 131 if(!_safePos(pos))exit(1); 132 if(this->_size==this->_capacity){ 133 reserve(this->_capacity+this->_size/3); 134 } 135 for(int j=this->_size;j>pos;j--){ 136 this->elems[j]=this->elems[j-1]; 137 } 138 this->elems[pos-1]=x; 139 } 140 void vector::erase(int pos){ 141 if(!this->_safePos(pos))exit(1); 142 for(int i=pos;i<this->_size-1;i++){ 143 this->elems[i]=this->elems[i+1]; 144 } 145 this->_size-=1; 146 } 147 void vector::push_back(int x){ 148 if(this->_capacity==this->_size){ 149 this->reserve(this->_capacity+this->_size/3); 150 this->elems[this->_size++]=x; 151 }else{ 152 this->elems[this->_size++]=x; 153 } 154 } 155 void vector::pop_back(){ 156 if(!this->empty()) 157 --this->_size; 158 } 159 int& vector::front(){ 160 return const_cast<int&>(static_cast<const vector&>(*this).front());//把指针转成const型,调用下面一个。再将结果的转成非const 161 } 162 const int& vector::front() const{ 163 return this->elems[0]; 164 } 165 int& vector::back(){ 166 return const_cast<int&>(static_cast<const vector*>(this)->back());//这里试试两种不同的写法而已 167 } 168 const int& vector::back() const{ 169 return this->elems[_size-1]; 170 } 171 bool vector::empty()const{ 172 return _size==0?true:false; 173 }
By Ginfoo