-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.h
127 lines (104 loc) · 2.33 KB
/
queue.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
#ifndef QUEUE_H
#define QUEUE_H
#include"deque.h"
namespace MyCppSTL
{
template<class T, class Sequence> class queue;
template<class T, class Sequence>
bool operator==(const queue<T, Sequence>&lhs,const queue<T, Sequence>&rhs);
template<class T, class Sequence>
bool operator<(const queue<T, Sequence>&lhs,const queue<T, Sequence>&rhs);
template<class T,class Sequence=MyCppSTL::deque<T>>
class queue
{
public:
//内嵌型别
typedef typename Sequence::value_type value_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
typedef typename Sequence::size_type size_type;
public:
friend bool operator==<>(const queue<T>&lhs,const queue<T>&rhs);
friend bool operator< <>(const queue<T>&lhs,const queue<T>&rhs);
public:
explicit queue(const Sequence&cont) :container(cont)
{
}
explicit queue(Sequence&&cont= Sequence()) :container(std::move(cont))
{
}
queue(const queue<T>&rhs) :container(rhs.container) //这里是调用底层容器的拷贝构造函数
{
}
queue(queue<T>&&rhs) :container(std::move(rhs.container)) //调用底层的move构造函数
{
}
~queue()
{
}
//
queue&operator=(const queue<T>&rhs)
{
if (this != &rhs)
{
container = rhs.container;
}
return *this;
}
reference front()
{
return container.front();
}
const_reference front() const
{
return container.front();
}
reference back()
{
return container.back();
}
const reference back() const
{
return container.back();
}
bool empty()
{
return container.empty();
}
size_type size()
{
return container.size();
}
void push(const value_type&value)
{
container.push_back(value);
}
void push(const value_type&&value)
{
container.push_back(std::move(value));
}
void pop()
{
container.pop_front();
}
void swap(queue<T>&rhs)
{
auto tmp = rhs.container;
rhs.container = container;
container = tmp;
}
private:
Sequence container; //底层容器实现
}; //end queue
template<class T,class Sequence=MyCppSTL::queue<T>>
bool operator==(const queue<T>&lhs,const queue<T>&rhs)
{
return (lhs.container == rhs.container);
}
template<class T, class Sequence>
bool operator<(const queue<T>&lhs,const queue<T>&rhs)
{
return (lhs.container < rhs.container);
}
} //end namespce
#endif //end queue.h