1.改变一维数组长度:
template <class T>
void changelength(T* &a, int oldlength, int newlength)
{
if (newlength < 0)
throw illegalparamentervalue("new length must be>=0");
T* temp = new T[newlength];
int number = min{ oldlength,newlength };
copy(a, a + number, temp);
delete[]a;
a = temp;
}
2.arraylist 定义
template <class T>
class arraylist :public linearlist<T>
{
public:
//构造函数,复制构造函数,析构函数
arraylist(int initialcapacity = 10);
arraylist(const arraylist<T>&);
~arraylist() { delete[]element; }
//ADT 方法
bool empty() const { return listsize == 0; }
int size() const { return listsize; }
T& get(int theindex) const;
int indexof(const T& the element) const;
void erase(int theindex);
void insert(int theindex, const T& theelement);
void output(ostream& out) const;
//其他方法
int capacity() const { return arraylength; }
protected:
void checkindex(int theindex) const;
T* element;
int arraylength;
int listsize;
};
3.arraylist 方法
- 构造函数
template <class T>
arraylist<T>::arraylist(int initialcapacity)
{
//构造函数
if (initialcapacity < 1)
{
ostringstream s;
s << "error";
}
arraylength = initialcapacity;
element = new T[arraylength];
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);
}
- 方法