-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
52 lines (43 loc) · 813 Bytes
/
stack.h
File metadata and controls
52 lines (43 loc) · 813 Bytes
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
/*
自定义一个栈结构,底层还是用STL
主要是为了POP操作的方便
重写后的栈POP时返回栈顶元素
*/
#ifndef STACK
#define STACK
#include <iostream>
#include <stack>
//栈,使用模板泛化
template<typename T>
class MyStack {
private:
std::stack<T> s;
public:
void push(const T& value)
{
s.push(value);
}
T pop() //主要修改了pop操作
{
if (s.empty())
{
throw runtime_error("栈为空");
}
T topValue = s.top();
s.pop();
return topValue;
}
T top() const
{
if (s.empty())
{
throw runtime_error("栈为空");
}
return s.top();
}
bool empty() const
{
return s.empty();
}
};
#endif