-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_template.cpp
69 lines (57 loc) · 1.54 KB
/
class_template.cpp
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
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
template <class T>
class Stack {
private:
vector<T> elems; //元素
public:
void push(T const&); // 入栈
void pop(); // 出栈
T top(); // 返回栈顶元素
bool empty() const { // 如果栈为空,则返回真
return elems.empty();
};
};
template <class T>
void Stack<T>::push(T const& elem) {
// 追加传入元素的副本
elems.push_back(elem);
};
template <class T>
void Stack<T>::pop() {
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// 删除最后一个元素
elems.pop_back();
};
template <class T>
T Stack<T>::top() {
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// 返回最后一个元素的副本
return elems.back();
};
int main() {
try {
Stack<int> intStack; // int类型的栈
Stack<string> stringStack; // string类型的栈
// 操作int 类型的栈
intStack.push(7);
cout << "intStack.top() : " << intStack.top() << endl;
// 操作string类型的栈
stringStack.push("class-template");
cout << "stringStack.top() : " << stringStack.top() << endl;
intStack.pop();
stringStack.pop();
// cout << "intStack.top() : " << intStack.top() << endl;
} catch (exception const& e) {
cerr << "Exception:" << e.what() << endl;
return -1;
}
}