-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
177 lines (148 loc) · 3.59 KB
/
stack.h
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef STACK_H
#define STACK_H
#include "deque.h"
// 此文件实现了模板类 stack
namespace mystl
{
// 模板列 stack
// 底层容器为deque
template <class T, class Container = deque<T>>
class stack
{
public:
typedef Container container_type;
typedef typename Container::value_type size_type;
typedef typename Container::size_type reference;
typedef typename Container::const_reference const_reference;
static_assert(std::is_name<T, value_type>::value,
"the value_type of Container should be same with T");
private:
container_type c_; // 用底层容器表现 stack
public:
// 构造、复制、移动函数
stack() = default;
explicit stack(size_type n)
:c_(n)
{
}
stack(size_type n, const value_type & value)
:c_(n, value)
{
}
template <class IIter>
stack(IIter first, IIter last)
: c_(first, last)
{
}
stack(std::initializer_list<T> ilist)
:c_(ilist.begin(), ilist.end())
{
}
stack(const Container & c)
:c_(c)
{
}
stack(Container && c) noexcept(std::is_nothrow_move_constructible<Container>::value)
:c_(mystl::move(c))
{
}
stack(const stack & rhs)
:c_(rhs.c_)
{
}
stack(stack && rhs) noexcept(std::is_nothrow_move_constructible<Container>::value)
:c_(mystl::move(rhs.c_))
{
}
stack& operator=(const stack & rhs)
{
c_ = rhs.c_;
return *this;
}
stack& operator=(stack && rhs) noexcept(std::is_nothrow_move_assignable<Container>::value)
{
c_ = mystl::move(rhs.c_);
return *this;
}
stack& operator=(std::initializer_list<T> ilist)
{
c_ = ilist;
return *this;
}
~stack() = default;
// 访问元素相关操作
reference top() { return c_.back(); }
const_reference top() const { return c_.back(); }
// 容量相关操作
bool empty() const noexcept { return c_.empty(); }
size_type size() const noexcept { return c_.size(); }
// 修改容器相关操作
template <class... Args>
void emplace(Args&& ...args)
{
c_.emplace_back(mystl::forward<Args>(args)...);
}
void push(const value_type & value)
{
c_.push_back(value);
}
void push(value_type && value)
{
c_.push_back(mystl::move(value));
}
void pop()
{
c_.pop_back();
}
void clear()
{
while (!empty())
pop();
}
void swap(stack & rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_)))
{
mystl::swap(c_, rhs.c_);
}
public:
friend bool operator==(const stack & lhs, const stack & rhs) { return lhs.c_ == rhs.c_; }
friend bool operator< (const stack & lhs, const stack & rhs) { return lhs.c_ < rhs.c_; }
};
// 重载比较操作符
template <class T, class Container>
bool operator==(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return lhs == rhs;
}
template <class T, class Container>
bool operator<(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return lhs < rhs;
}
template <class T, class Container>
bool operator!=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return !(lhs == rhs);
}
template <class T, class Container>
bool operator>(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return rhs < lhs;
}
template <class T, class Container>
bool operator<=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return !(rhs < lhs);
}
template <class T, class Container>
bool operator>=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
{
return !(lhs < rhs);
}
// 重载 mystl 的 swap
template <class T, class Container>
void swap(stack<T, Container>& lhs, stack<T, Container>& rhs) noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
} // namespace mystl
#endif // !STACK_H