抽象基类的定义 linearList.h :
1 #pragma once
2 #include <iostream>
3
4 template<typename T>
5 class linearList
6 {
7 public:
8 virtual ~linearList() {};
9 virtual bool empty() const = 0;
10 virtual int size() const = 0;
11 virtual T &get(int Index) const = 0;
12 virtual int indexOf(const T &theElement) const = 0;
13 virtual void erase(int theIndex) = 0;
14 virtual void insert(int theIndex, T &theElement) = 0;
15 virtual void output(std::ostream& out) = 0;
16 };
异常类的定义在 myExceptions.h 中:
1 #pragma once
2 // exception classes for various error types
3 #include <string>
4
5 using namespace std;
6
7 // illegal parameter value
8 class illegalParameterValue
9 {
10 public:
11 illegalParameterValue(string theMessage = "Illegal parameter value")
12 {
13 message = theMessage;
14 }
15 void outputMessage() { cout << message << endl; }
16 private:
17 string message;
18 };
19
20 // illegal input data
21 class illegalInputData
22 {
23 public:
24 illegalInputData(string theMessage = "Illegal data input")
25 {
26 message = theMessage;
27 }
28 void outputMessage() { cout << message << endl; }
29 private:
30 string message;
31 };
32
33 // illegal index
34 class illegalIndex
35 {
36 public:
37 illegalIndex(string theMessage = "Illegal index")
38 {
39 message = theMessage;
40 }
41 void outputMessage() { cout << message << endl; }
42 private:
43 string message;
44 };
45
46 // matrix index out of bounds
47 class matrixIndexOutOfBounds
48 {
49 public:
50 matrixIndexOutOfBounds
51 (string theMessage = "Matrix index out of bounds")
52 {
53 message = theMessage;
54 }
55 void outputMessage() { cout << message << endl; }
56 private:
57 string message;
58 };
59
60 // matrix size mismatch
61 class matrixSizeMismatch
62 {
63 public:
64 matrixSizeMismatch(string theMessage =
65 "The size of the two matrics doesn't match")
66 {
67 message = theMessage;
68 }
69 void outputMessage() { cout << message << endl; }
70 private:
71 string message;
72 };
73
74 // stack is empty
75 class stackEmpty
76 {
77 public:
78 stackEmpty(string theMessage =
79 "Invalid operation on empty stack")
80 {
81 message = theMessage;
82 }
83 void outputMessage() { cout << message << endl; }
84 private:
85 string message;
86 };
87
88 // queue is empty
89 class queueEmpty
90 {
91 public:
92 queueEmpty(string theMessage =
93 "Invalid operation on empty queue")
94 {
95 message = theMessage;
96 }
97 void outputMessage() { cout << message << endl; }
98 private:
99 string message;
100 };
101
102 // hash table is full
103 class hashTableFull
104 {
105 public:
106 hashTableFull(string theMessage =
107 "The hash table is full")
108 {
109 message = theMessage;
110 }
111 void outputMessage() { cout << message << endl; }
112 private:
113 string message;
114 };
115
116 // edge weight undefined
117 class undefinedEdgeWeight
118 {
119 public:
120 undefinedEdgeWeight(string theMessage =
121 "No edge weights defined")
122 {
123 message = theMessage;
124 }
125 void outputMessage() { cout << message << endl; }
126 private:
127 string message;
128 };
129
130 // method undefined
131 class undefinedMethod
132 {
133 public:
134 undefinedMethod(string theMessage =
135 "This method is undefined")
136 {
137 message = theMessage;
138 }
139 void outputMessage() { cout << message << endl; }
140 private:
141 string message;
142 };
vectorList 类的定义 vectorList.h :
1 #pragma once
2 #include <iostream>
3 #include <sstream>
4 #include <iterator>
5 #include <string>
6 #include <algorithm>
7 #include <vector>
8 #include "linearList.h"
9 #include "myExceptions.h"
10
11
12 template<typename T>
13 class vectorList : public linearList<T>
14 {
15 public:
16 // vectorList() = default;
17 vectorList(int initSize = 10);
18 vectorList(const vectorList<T> &cVectorList);
19 ~vectorList();
20 bool empty() const;
21 int size() const;
22 T &get(int Index) const;
23 int indexOf(const T &theElement) const;
24 void erase(int theIndex);
25 void insert(int theIndex, T &theElement);
26 void output(std::ostream& out);
27
28 int capacity() const { return element->capacitp(); }
29 protected:
30 void checkIndex(int theIndex) const;
31 std::vector<T> *element;
32 };
33
34 template<typename T>
35 vectorList<T>::vectorList(int initSize)
36 {
37 if (0 > initSize)
38 {
39 std::ostringstream s;
40 s << "initSize = " << initSize << "must > 0" << endl;
41 throw illegalParameterValue(s.str());
42 }
43 element = new vector<T>(initSize);
44 }
45
46 template<typename T>
47 vectorList<T>::vectorList(const vectorList<T> &cVectorList)
48 {
49 element = new std::vector<T>(*cVectorList.element);
50 }
51
52 template<typename T>
53 vectorList<T>::~vectorList()
54 {
55 delete element;
56 }
57
58 template<typename T>
59 void vectorList<T>::checkIndex(int theIndex) const
60 {
61 if (theIndex < 0 || theIndex >= size())
62 {
63 std::ostringstream s;
64 s << "theIndex = " << theIndex << "error !" << endl;
65 throw illegalIndex(s.str());
66 }
67 }
68
69 template<typename T>
70 bool vectorList<T>::empty() const
71 {
72 return element->empty();
73 }
74
75 template<typename T>
76 int vectorList<T>::size() const
77 {
78 return element->size();
79 }
80
81 template<typename T>
82 T &vectorList<T>::get(int Index) const
83 {
84 checkIndex(Index);
85 //return element[Index];
86 return *(element->begin() + Index);
87 }
88
89 template<typename T>
90 int vectorList<T>::indexOf(const T &theElement) const
91 {
92 int Index = -1;
93 for (int i = 0; i < element->size(); i++)
94 {
95 if (*(element->begin() + i) == theElement)//(element[i] == theElement)
96 {
97 Index = i;
98 break;
99 }
100 }
101
102 return Index;
103 }
104
105 template<typename T>
106 void vectorList<T>::erase(int theIndex)
107 {
108 checkIndex(theIndex);
109 element->erase(element->begin() + theIndex);
110 }
111
112 template<typename T>
113 void vectorList<T>::insert(int theIndex, T &theElement)
114 {
115 if (theIndex < 0 || theIndex > size())
116 {
117 std::ostringstream s;
118 s << "theIndex = " << theIndex << "error !" << endl;
119 throw illegalIndex(s.str());
120 }
121 element->insert(element->begin() + theIndex, theElement);
122 }
123
124 template<typename T>
125 void vectorList<T>::output(std::ostream& out)
126 {
127 copy(element->begin(), element->end(), std::ostream_iterator<T>(out, " "));
128 }
129
130 template<typename T>
131 std::ostream &operator<<(std::ostream &cout, vectorList<T> &cVectorList)
132 {
133 cVectorList.output(cout);
134 return cout;
135 }
参考文献:
[1].Sartaj Sahni. 数据结构、算法与应用[M]. 机械工业出版社, 2000.