用了双向链表,快排,<<,=,[]重载,还有erase的实现比较好玩
//my Vecter ;T need "operator<" #include <iostream>
using std::cout;
using std::ostream; template <typename T>
struct item
{
item():value(),next(NULL),last(NULL){}
item(const T t):value(t),next(NULL),last(NULL){}
item *next,*last;
T value;
}; template <typename T>
class Vector
{
public:
Vector():m_size(),m_head(NULL){}
int size() const{return m_size;}
bool empty() const{return m_size?true:false;}
T front() const{return m_size?m_head->value:T();}
T back() const{return m_size?end()->value:T();}
bool push_back(const T&);
bool push_front(const T&);
T pop_back();
T pop_front();
void erase(int pos,int num);
void clear();
T& operator[](const int key);
void sort();
Vector<T>& operator=(Vector<T>& v); private:
item<T> * end() const;
int m_size;
item<T> *m_head;
int partition(int,int);
void qsort(int,int);
}; template <typename T>
bool Vector<T>::push_back(const T& t)
{
if(m_size==)
{
m_head=new item<T>;
m_head->value=t;
}
else
{
item<T>* save=end();
save->next=new item<T>(t);
save->next->last=save;
}
m_size++;
return true;
} template <typename T>
bool Vector<T>::push_front(const T& t)
{
if(m_size==)
{
m_head=new item<T>(t);
return true;
}
item<T> *save=m_head;
m_head=new item<T>(t);
m_head->next=save;
m_size++;
if(save!=NULL)save->last=m_head;
return true;
} template <typename T>
T Vector<T>::pop_front()
{
if(m_size==)return T();
if(m_size==)
{
T t=m_head->value;
delete m_head;
m_head=NULL;
m_size--;
return t;
}
item<T>* save=m_head->next;
T t=m_head->value;
delete m_head;
m_head=save;
save->last=m_head;
m_size--;
return t;
} template <typename T>
T Vector<T>::pop_back()
{
if(m_size==)return T();
if(m_size==)
{
T t=m_head->value;
delete m_head;
m_head=NULL;
m_size--;
return t;
}
item<T>* e=end();
T t=e->value;
e->last->next=NULL;
delete e;
m_size--;
return t;
} template <typename T>
void Vector<T>::erase(int pos,int num)
{
if(m_size<pos+)return;
else
{
item<T> *p=m_head,*save;
for(int i=;i<pos;i++){p=p->next;}
for(int i=;i<num;i++)
{
if(p==NULL)break;
save=p;
if(p->last==NULL){m_head=p->next;}
else p->last->next=p->next;
if(p->next!=NULL)p->next->last=p->last;
p=p->next;
delete save;
m_size--;
}
}
} template <typename T>
T& Vector<T>::operator[](const int key)
{
if(key+>m_size)m_size=(key+);
item<T> *p=m_head,*save;
for(int i=;i<key+;i++)
{
if(m_head==NULL)
{
m_head=new item<T>;
save=p=m_head;
}
else if(p==NULL)
{
p=new item<T>;
p->last=save;
save->next=p;
}
save=p;
p=p->next;
}
return save->value;
} template <typename T>
void Vector<T>::clear()
{
erase(,m_size);
} template <typename T>
item<T>* Vector<T>::end() const
{
if(m_size==)return NULL;
item<T> *p=m_head;
for(; p->next!=NULL; p=p->next);
return p;
} template <typename T>
int Vector<T>::partition(int p,int r)
{
T x=(*this)[r];
int i=p-;T temp;
for(int j=p;j<=r-;j++)
{
if((*this)[j]<x)
{
i++;
if(i!=j)
{temp=(*this)[i];(*this)[i]=(*this)[j];(*this)[j]=temp;}
}
}
if(r!=i+)
{temp=(*this)[r];(*this)[r]=(*this)[i+];(*this)[i+]=temp;}
return i+;
} template <typename T>
void Vector<T>::sort()
{
qsort(,m_size-);
}
template <typename T>
void Vector<T>::qsort(int p,int r)
{
if(p<r)
{
int q=partition(p,r);
qsort(p,q-);
qsort(q+,r);
}
} template<typename T>
ostream& operator<<(ostream& out,Vector<T> &v)
{
for(int i=;i<v.size();i++)
{
out<<v[i]<<" ";
}
return out;
}
template<typename T>
Vector<T>& Vector<T>::operator=(Vector& v)
{
this->clear();
m_size=v.m_size;
item<T> *p=m_head,*vp=v.m_head;
for(int i=;i<m_size;i++)
{
p=new item<T>(vp->value);
p=p->next;
vp=vp->next;
}
return *this;
} int main()
{
int i=;
Vector<int> v;
v.push_back(i);i--;
v.push_front(i);
v.push_back(i);i--;
v.push_front(i);
v[]=;
cout<<v<<"\n";
v.erase(,);
cout<<v<<"\n";
v.clear();
for(int i=;i>;i--)v[i]=-i;
v[]=;
v[]=;
cout<<v<<"\n";
v.sort();
cout<<"V1= "<<v<<"\n";
Vector<int> v2=v;
cout<<"V2= "<<v2<<"\n";
return ;
}