c++arrayList的实现,arraylist实现
c++arrayList的实现,arraylist实现
数据结构:数据结构是一个数据对象,同时这个对象的实例以及构成实例的元素都存在这联系,而这些联系由相关的函数来规定。
C++中存在着许多基本的数据类型,比如 int,bool 等,数据结构就是将这些基本数据类型通过函数和相应的规则组合起来形成新的数据类型。
抽象类LinearList:
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const=0;
virtual int size() const=0;
virtual int indexOf(const T& theElement) const=0;
virtual T& get(int theIndex) const=0;
virtual void erase(int theIndex)=0;
virtual void insert(int theIndex,const T& theElement)=0;
virtual void output(ostream& out)const=0;
}
抽象类LinearList大量使用了纯虚函数,规定了一个线性数组必须包含的常用的函数,比如插入,删除等。包含
纯虚函数的类不能实例化,所以它的继承类必须实现所有的纯虚函数。这里析构函数写成了虚函数的形式,在类中定义虚函数的时候,类会自动生成一个虚函数表,当子类的函数调用虚函数时,系统会首先调用基类的该虚函数,然后虚函数表会根具调用该函数的子类优先调用相应的子类的析构函数,所以保证在调用基类析构函数之前优先调用子类的析构函数,防止内存泄漏。
arrayList类:
template<class T>
class arrayList:public linearList<T>
{
public:
arrayList(int initialCapacity=10);
arrayList(const arrayList<T>&);
~arrayList(){delete [] element};
bool empty() const {return listSize==0;}
int size() const {return listSize;}
T& get(int theIndex)const;
int indexOf(const T& theElement)const;
void erase(int theIndex);
void insert(int theIndex,const T& theElement);
void output(ostream& out)const;
int capacity(){return arrayLength;}
protected:
void checkIndex(int theIndex)const;
T* element;
int arrayLength;
int listSize;
};
上面的arrayList类重写了基类的所有纯虚函数,并且增添了很多其他的函数。
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{
if(initialCapacity<1)
{
ostringstream s;
s<<"initial capacity="<<initialCapacity<<"Must be>0";
throw illegalParameterValue(s.str());
}
arrayLength=initialCapacity;
element=new T[arrayLength];
listSize=0;
}
上面是构造函数,ostringstream是字符串输入输出流,首先将字符串输入到ostringstream流中,然后通过str()函数取出。
然后根据根据指定数组大小new出一个连续的新内存,初始化大小listSize为0;
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
arrayLength=theList.arrayLength;
listSize=theList.listSize;
element=new T[arrayLength];
copy(theList.element,theList.element+listSize,element);
}
上面为复制构造函数,很简单,不过使用了stl里面的copy函数,前面有讲过这个函数,这里就不说了。
补充一点:类的成员函数可以直接访问类的实例里面的私有变量,并且对于使用的new的类,构造函数请一定不要使用默认复制构造函数。
template<class T>
void arrayList<T>::checkIndex(int theIndex)const
{
if(theIndex<0||theIndex>listSize)
{
ostringstream s;
s<<"index="<<theIndex<<"size="<<listSize;
throw illegalIndex(s.str());
}
}
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(theElement==listSize)
return -1;
else
return theIndex;
}
由于我们直接new出了一个数组,所以我们默认数组为基本数据类型,所以我们这里直接使用了element[theIndex]和find()的stl函数,而没有自定义operator[]和find()函数。
这里顺便介绍一下stl中find的函数吧:
1.find(first, end, value);
返回区间[first,end]中第一个值等于value的元素的位置,找不到则返回end。函数返回的是迭代器和指针即位置信息。
2.find_if(first, end, bool pred);
返回一元判断式中满足pred为true的在区间[first,end]中的第一个元素的位置,找不到返回end。
3.find_first_of(first1, end1, first2, end2);
返回区间[first1,end1]中与区间[first2,end2]重复的第一个元素的位置。
即选中第一个区间的第一个元素,看第二个区间有没有这个元素,如果有则返回位置,没有继续第二个元素,依次查找。
template<class T>
void arrayList<T>::erase(int theIndex)
{
checkIndex(theIndex);
copy(element+theIndex+1,element+listSize,element+theIndex);
element[--listSize].~T();
}
在数组里面删除一个元素其实就是将它右边的数据往左边挪一下,最后那个析构其实可以不调用。
template<class T>
void changeLength1D(T*& a,int oldLength,int newLength)
{
if(newLength<0)
throw illegalParameterValue("new length must be>=0");
T* temp=new T[newLength];
int number=min(oldLength,newLength);
copy(a,a+number,temp);
delete [] a;
a=temp;
}
template<class T>
void arrayList<T>::insert(int theIndex,const T& theElement)
{
if(theIndex<0||theIndex>listSize)
{
ostringstream s;
s<<"index="<<theIndex<<"size="<<listSize;
throw illegalIndex(s.str())
}
if(listSize==arrayLength)
{
changeLength1D(element,arrayLength,2*arrayLength);
arrayLength*=2;
}
copy_backward(element+theIndex,element+listSize,element+listSize+1);
element[theIndex]=theElement;
}
上面这个就是插入的代码了,插入和删除一样都是移动元素,插入是将theIndex以后的元素全部向后移动一位,空出theIndex
然后在theIndex这个位置插入我们要插入的代码,不过这里有一个问题,那就是如果原数组是满的那么我们就不能执行插入操作,我们就得先将原数组的长度扩大一倍,我们是new了一个新的数组长度为原来的一倍,并且删除之前的数组内存,并将旧内存的指针指向新内存。
template<class T>
void arrayList<T>::output(cout->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类就差不多写好了。
相关文章
- 暂无相关文章
用户点评