**myExceptions.h**
#pragma once
#include<iostream>
using namespace std;
#include<string>
class illegalParameterValue {
public:
illegalParameterValue(string theMessage = " Illegal Parameter Value") {
message = theMessage;
}
void outputMessage() { cout << message << endl; }
private:
string message;
};
class illegalIndex {
public:
illegalIndex(string theMessage = " Illegal Index") {
message = theMessage;
}
void outputMessage() { cout << message << endl; }
private:
string message;
};
**linearList.hpp**
#pragma once
#include<iostream>
using namespace std;
template<class T>
class linearList {
public:
virtual ~linearList() {}
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int theIndex) const = 0;
virtual int indexOf(const T& theElement) const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex, const T& theElement) = 0;
virtual void output(ostream& out) const = 0;
};
arrayList.hpp
#pragma once
#include "linearList.hpp"
#include<algorithm>
#include<sstream>
#include "myExceptions.h"
template<class T>
class arrayList :public linearList<T>{
public:
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>& theList);
~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() const { return arrayLength; }
protected:
void checkIndex(int theIndex)const;
T* element;
int arrayLength;
int listSize;
};
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;
}
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);
}
template<class T>
void arrayList<T>::checkIndex(int theIndex)const {
if (theIndex < 0 || theIndex > listSize - 1) {
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(theIndex == listSize)
return -1;
return theIndex;
}
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 (listSize == arrayLength) {
changeLength1D(element, arrayLength, arrayLength * 2);
arrayLength *= 2;
}
copy_backward(element + theIndex, element + listSize, element + listSize + 1);
element[theIndex] = theElement;
}
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;
}
**main.cpp**
#include"arrayList.hpp"
int main()
{
//创建两个容量为100的线性表
linearList<int>* x = new arrayList<int>(100);
arrayList<int> y(100);
cout << y.get(2) << endl;
system("pause");
return 0;
}

