1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| #include<iostream>
#include<vector>
#include<sstream>
using namespace std;
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 outputMessge() {
cout << message << endl;
}
private:
string message;
};
template<class T>
class vectorList {
public:
// 设置友好类,来重载<<
// 构造函数,复制构造函数和构析函数
explicit vectorList(int initialCapacity = 10);
vectorList(const vectorList<T> &);
~vectorList() { delete element; }
// ADT方法
bool empty() const { return element->empty(); }
int size() const { return element->size(); }
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 (int) element->capacity(); }
// 线性表的起始和结束位置的迭代器
typedef typename vector<T>::iterator iterator;
iterator begin() const { return element->begin(); }
iterator end() const { return element->end(); }
protected:
void checkIndex(int theIndex) const;
vector<T> *element;
};
template<class T>
vectorList<T>::vectorList(int initialCapacity) {
// 构造函数
if (initialCapacity < 1) {
ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
element = new vector<T>;
element->reserve(initialCapacity); // vector容量从0增加到initialCapacity
}
template<class T>
vectorList<T>::vectorList(const vectorList<T> &theList) {
// 复制构造函数
element = new vector<T>(*theList.element);
}
template<class T>
void vectorList<T>::checkIndex(int theIndex) const {
if (theIndex < 0 || theIndex >= Capacity()) {
stringstream s;
s << "index = " << theIndex << " size = " << Capacity();
throw illegalParameterValue(s.str());
}
}
template<class T>
void vectorList<T>::erase(int theIndex) {
// 删除索引为theIndex的元素
// 如果没有这个元素,则抛出异常
checkIndex(theIndex);
element->erase(begin() + theIndex);
}
template<class T>
void vectorList<T>::insert(int theIndex, const T &theElement) {
// 在索引为theIndex处插入元素theElement
checkIndex(theIndex);
element->insert(element->begin() + theIndex, theElement);
}
// 重载<<运算符输出
template<class T>
ostream &operator<<(ostream &out, const vectorList<T> &x) {
for (auto i: x)
out << i << '\t';
return out;
}
int main() {
auto *array1 = new vectorList<int>(20);
array1->insert(0, 1);
array1->insert(1, 2);
array1->insert(2, 3);
array1->insert(3, 4);
cout << array1->size() << endl;
cout << *array1 << endl;
array1->insert(20, 30);
return 0;
}
|