-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack2.hpp
36 lines (32 loc) · 946 Bytes
/
stack2.hpp
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
#include "../2_1/stack1.hpp"
#include <deque>
#include <string>
#include <cassert>
template<>
class Stack<std::string>
{
private:
std::deque<std::string> elems; // 元素
public:
void push(std::string const& elem); // 插入一个元素
void pop(); // 推出一个元素
std::string const& top() const; // 返回栈顶元素
bool empty() const // 返回栈是否为空
{
return elems.empty();
}
};
void Stack<std::string>::push(std::string const& elem)
{
elems.push_back(elem); // 将传递的 elem 元素的拷贝进行插入
}
void Stack<std::string>::pop()
{
assert(!elems.empty());
elems.pop_back(); // 移除栈底元素
}
std::string const& Stack<std::string>::top() const
{
assert(!elems.empty());
return elems.back(); // 返回栈顶元素的引用
}