my_vector模板类
template <typename T>
class my_vector{
public:
my_vector():first(NULL),last(NULL),end(NULL){}
my_vector(int num,T data):first(new T[num]){
last=first;
end=first+num-1;
int i=0;
while(i<num){
*last=data;
last++;
i++;
}
}
my_vector(const my_vector& v):first(new T[v.end-v.first+1]){
memcpy(first,v.first,(v.end-v.first+1)*sizeof(T));
last=first+(v.last-v.first);
end=first+(v.end-v.first);
}
my_vector& operator=(const my_vector& v){
if(first!=NULL){ //释放原有内存
delete []first;
first=NULL;
last=NULL;
end=NULL;
}
first=new T[v.end-v.first];
memcpy(first,v.first,sizeof(T)*(v.end-v.first));
last=first+(v.last-v.first);
end=first+(v.end-v.first);
}
bool operator==(const my_vector& v)const{
int i=0;
while(i<(v.end-v.first)){
if(first[i]==v.first[i]&&(end-first)==(v.end-v.first)){
}else{
return false;
}
i++;
}
return true;
}
bool operator!=(const my_vector& v)const{
int i=0;
int len=end-first;
int len_v=v.end-v.first;
int res=len>len_v?len:len_v;
while(i<res){
if(first[i]!=v.first[i]&&(end-first)!=(v.end-v.first)){
return true;
}
i++;
}
return false;
}
bool operator>(const my_vector& v)const{
int i=0;
int len=end-first;
int len_v=v.end-v.first;
int res=len>len_v?len:len_v;
while(i<res){
if(first[i]>v.first[i]){
return true;
}
i++;
}
return false;
}
bool operator<(const my_vector& v)const{
int i=0;
int len=end-first;
int len_v=v.end-v.first;
int res=len>len_v?len:len_v;
while(i<res){
if(first[i]<v.first[i]){
return true;
}
i++;
}
return false;
}
bool operator<=(const my_vector& v)const{
int i=0;
int len=end-first;
int len_v=v.end-v.first;
int res=len>len_v?len:len_v;
while(i<res){
if(first[i]<=v.first[i]){
return true;
}
i++;
}
return false;
}
bool operator>=(const my_vector& v)const{
int i=0;
int len=end-first;
int len_v=v.end-v.first;
int res=len>len_v?len:len_v;
while(i<res){
if(first[i]>=v.first[i]){
return true;
}
i++;
}
return false;
}
bool empty(){
if(first==last){
return true;
}else{
return false;
}
}
T& at(int num){
return first[num-1];
}
int size(){
return last-first;
}
int capacity(){
return end-first+1;
}
void clear(){
while(last>first){
last--;
}
}
void push_back(T data){
int total_maxsize=end-first+1;
int total_len=last-first;
if(1+(last-first)>(end-first+1)){ //满
T *temp=new T[last-first]; //将原始值存入temp
memcpy(temp,first,(last-first)*sizeof(T));
delete []first; //释放原始内存
first=NULL;
last=NULL;
end=NULL;
first =new T[total_maxsize*2];
memcpy(first,temp,total_len*(sizeof(T)));
last=first+total_len;
end=first+total_maxsize*2-1;
*last=data;
last++;
delete []temp;
temp=NULL;
}else{
*last=data;
last++;
}
}
void pop_back(){
if(last==first){
cout<<"VECTOR IS NULL"<<endl;
}else{
last--;
}
}
T* begin(){
return first;
}
T* get_end(){
return (last-1);
}
T& back(){
return *(last-1);
}
T& front(){
return *first;
}
int max_size(){
return end-first+1;
}
T* erase(int loc){
for(int i=loc-1;i<last-first-1;i++){
first[i]=first[i+1];
}
last--;
return first+loc-1;
}
T* erase(T* start,T* en){
for(int i=0;i<last-first;i++){
*(start+i)=*(en+i);
}
for(int i=0;i<en-start;i++){
last--;
}
return last+1;
}
void insert(int loc,int num,T data){
int total_maxsize=end-first+1;
int total_len=last-first;
if(num+(last-first)>(end-first+1)){ //满
T *temp=new T[last-first]; //将原始值存入temp
memcpy(temp,first,(last-first)*sizeof(T));
delete []first; //释放原始内存
first=NULL;
last=NULL;
end=NULL;
first =new T[total_maxsize*2];
memcpy(first,temp,(loc-1)*sizeof(T));
for(int i=loc-1;i<loc-1+num;i++){
first[i]=data;
}
memcpy(first+loc-1+num,temp+loc-1,(total_len-(loc-1))*sizeof(T));
last=first+total_len+num;
end=first+total_maxsize*2-1;
for(int i=0;i<num;i++){
last++;
}
delete []temp;
temp=NULL;
}else{
for(int i=0;i<num;i++) {
for(int j=total_len-1+i;j>loc-1+i;j--){
first[j+1]=first[j];
}
first[loc+i]=data;
}
for(int i=0;i<num;i++){
last++;
}
}
}
void insert(int loc,T* s,T* e){
int total_maxsize=end-first+1;
int total_len=last-first;
if((e-s)+(last-first)>(end-first+1)){
T *temp=new T[last-first]; //将原始值存入temp
memcpy(temp,first,(last-first)*sizeof(T));
delete []first; //释放原始内存
first=NULL;
last=NULL;
end=NULL;
first =new T[total_maxsize*2];
memcpy(first,temp,(loc-1)*sizeof(T));
for(int i=loc-1;i<loc-1+(e-s);i++){
first[i]=*(s+i);
}
memcpy(first+loc-1+(e-s),temp+loc-1,(total_len-(loc-1))*sizeof(T));
last=first+total_len+(e-s);
end=first+total_maxsize*2-1;
for(int i=0;i<e-s;i++){
last++;
}
delete []temp;
temp=NULL;
}else{
for(int i=0;i<e-s;i++) {
for(int j=total_len-1+i;j>loc-1+i;j--){
first[j+1]=first[j];
}
first[loc+i]=*(s+i);
}
for(int i=0;i<e-s;i++){
last++;
}
}
}
void assgin(int num,T data){
T *temp=new T[last-first]; //将原始值存入temp
memcpy(temp,first,(last-first)*sizeof(T));
delete []first; //释放原始内存
first=NULL;
last=NULL;
end=NULL;
first =new T[num*sizeof(T)];
for(int i=0;i<num;i++){
first[i]=data;
}
last=first+num;
end=first+num-1;
delete [] temp;
temp=NULL;
}
void assgin(T* s,T* e){
T *temp=new T[last-first];
memcpy(temp,first,(last-first)*sizeof(T));
delete []first; //释放原始内存
first=NULL;
last=NULL;
end=NULL;
first =new T[(e-s)*sizeof(T)];
for(int i=0;i<e-s;i++){
*(first+i)=*(s+i);
}
last=first+(e-s);
end=first+(e-s)-1;
delete []temp;
temp=NULL;
}
void show(){
T *temp=first;
while(temp<last){
cout<<*temp<<" ";
temp++;
}
cout<<endl;
}
void show_mem(){
cout<<"first-->"<<*first<<endl;
cout<<"last-->"<<*last<<endl;
cout<<"len="<<last-first<<endl;
cout<<"maxsize="<<end-first+1<<endl;
}
private:
T *first;
T *last;
T *end;
};
主函数
int main()
{
my_vector<int> v1(1,1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.push_back(6);
v1.insert(3,2,7);
v1.show();
v1.erase(3);
int *p=v1.begin();
int *q=v1.get_end();
my_vector<int> v2(10,1);
v2.assgin(p,q);
v2.show();
v2.show_mem();
cout<<*p<<endl;
cout<<*q<<endl;
v1.erase(p,q);
v2.show();
cout<<"v1.back()="<<v1.back()<<endl;
cout<<"v1.front()="<<v1.front()<<endl;
cout<<"v1.max_size()="<<v1.max_size()<<endl;
// v1.show();
// v1.show_mem();
v1.show();
cout<<"====================="<<endl;
my_vector<int> v4(v1);
//v2.show_mem();
v2.show();
my_vector<int> v3=v1;
cout<<"====================="<<endl;
v3.show();
cout<<"====================="<<endl;
cout<<(v1==v3)<<endl;
cout<<"====================="<<endl;
cout<<v4.at(3)<<endl;
cout<<"====================="<<endl;
cout<<v4.capacity()<<endl;
cout<<v4.size()<<endl;
cout<<"====================="<<endl;
v4.clear();
v4.show();
v1.show_mem();
return 0;
}