#include<iostream>
#include <sstream>
#include<iterator>
#include<algorithm>
using namespace std;
template<class iterator>
void copy(iterator start, iterator end, iterator to) {
while (start != end)
*(to++) = *(start++);
}
template<class T>
class linearList {
public:
virtual ~linearList() = default;;
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;
};
class illegalParameterValue {
public:
illegalParameterValue() : message("Illegal parameter value") {}
explicit illegalParameterValue(char *theMessage) {
message = theMessage;
}
explicit illegalParameterValue(const string &theMessage) {
message = theMessage;
cout << message << endl;
}
void outputMessage() { cout << message << endl; }
private:
string message;
};
template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
if (newLength < 0)
throw illegalParameterValue("new length muse be >= 0");
T *temp = new T[newLength];
int number = min(oldLength, newLength);
copy(a, a + number, temp);
delete[] a;
a = temp;
}
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() const { return arrayLength; }
protected:
void checkIndex(int theIndex) const;
T *element;
int arrayLength;
int listSize;
};
template<class T>
void arrayList<T>::checkIndex(int theIndex) const {
if (theIndex < 0 || theIndex >= listSize) {
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalParameterValue(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;
else
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>
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>::insert(int theIndex, const T &theElement) {
if (theIndex < 0 || theIndex > listSize) {
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalParameterValue(s.str());
}
if (listSize == arrayLength) {
changeLength1D(element, arrayLength, arrayLength * 2);
arrayLength *= 2;
}
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;
}
int main() {
auto *array1 = new arrayList<int>(100);
arrayList<double> y(100);
cout << *array1 << endl;
cout << y << endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律