码迷,mamicode.com
首页 > 其他好文 > 详细

实现arrayList

时间:2015-10-01 21:43:09      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
using namespace std;

template<class T>
class myIterator
{
public:
    myIterator(T*thePosition=0):position(thePosition){};
    T&operator*(){return *position;}
    T*operator->(){return &*position;}
    myIterator&operator++()
    {
        ++position;
        return *this;
    }
    myIterator operator++(int)
    {
        myIterator old=*this;
        ++position;
        return old;
    }
    myIterator&operator--()
    {
        --position;
        return *this;
    }
    myIterator operato(int)
    {
        myIterator old=*this;
        --position;
        return old;
    }
    bool operator==(const myIterator right)const
    {
        return position==right.position;
    }
    bool operator!=(const myIterator right)const
    {
        return position!=right.position;
    }
protected:
    T*position;
};
template<class T>
class linearList
{
public:
    virtual ~linearList(){};
    virtual bool empty()const=0;
    virtual int size()const=0;
    virtual T& get(int theIdex)const=0;
    virtual void erase(int theIdex)=0;
    virtual void insert(int theIdex,const T&theElement)=0;
    virtual void output(ostream&out)const=0;
};
template<class T>
void changeLength1D(T*&a,int oldlength,int newlength)
{
    if(newlength<0)
        throw runtime_error("new length must be large than or equal 0");
    T *temp=new T[newlength];
    int num=min(oldlength,newlength);
    copy(a,a+num,temp);
    delete []a;
    a=temp;
}
template<class T>
class arrayList:public linearList<T>
{
public:
    arrayList(int initialCapacity=10);
    arrayList(const arrayList<T>&);
    
    ~arrayList(){};
    bool empty()const{return listSize==0}
    int size()const{return listSize}
    T& get(int theIdex)const;
    int indexOf(const T&theElement)const;
    void erase(int theIdex);
    void insert(int theIdex,const T&theElement);
    void output(ostream&out)const;
    int capacity()const{return arrayLength}
    myIterator<T> begin(){return myIterator(element);}
    myIterator<T> end(){return myIterato(element+listSize);}
protected:
    void checkIndex(int theInndex)const;
    int arrayLength;
    int listSize;
    T*element;
};
template<class T>
arrayList<T>::arrayList(int initialCapacity=10)
{
    if(initialCapacity<=0)
        throw runtime_error("Capacity must large than 0");
    element=new T[initialCapacity];
    arrayList=initialCapacity;
    listSize=0;
}
template<class T>
arrayList<T>::arrayList(const arrayList<T>&theList)
{
    listSize=theList.listSize();
    arrrylength=theList.arrayLength();
    element=new T[arraylength];
    copy(theList.element,theList.element+theList.listSize(),element);
}

template<class T>
void arrayList<T>::checkIndex(int theIndex)const
{
    if(theIndex<0||theIndex>=listSize)
        throw runtime_error("out of range");
}
template<class T>
T& arrayList<T>::get(int theIndex)const
{
    checkIndex(theIndex);
    return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T&theElement)const
{
    int theIndex=(int)(find(element,element+listSize,theElement)-element);
    if(theIndex==listSize)
        return -1;
    else return theIndex;
}
template<class T>
void arrayList<T>::erase(int theIndex)
{
    check(theIndex);
    copy(element+theIndex+1,element+listSize,element+theIndex);
    element[--listSize].~T();
}
template<class T>
void arrayList<T>::insert(int theIndex,const T&theElement)
{
    if(theIndex>listSIze||theIndex<0)
        throw runtime_error("insert out of rang");
    if(listSize==arrayLength)
        changeLength1D(element,arrayLength,2*arraylength);
    //copy(element+theIndex,element+listSize,element+theIndex+1);
    copy_backward(element+theIndex,element+listSize,element+listSize+1);
    element[theIndex]=theElement;
    listSize++;
}
template<class T>
void arrayList<T>::output(ostream&out)const
{
    copy(element,element+listSize,ostream_iterator<T>(cout," "));
}
template<class T>
ostream&operator<<(ostream&out,const arrayList<T> &x)
{
    x.output(out);
    return out;
}

 

实现arrayList

标签:

原文地址:http://www.cnblogs.com/zhangyee/p/4851503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!