前言:栈和队列被称为 “适配器”,类比生活中的电源适配器,它不产生电(不自己存储数据),而是转换电为特定的大小(以特定方式管理数据)以便使用
而这种 “将一个类的接口转换成客户希望的另外一个接口” 的方式,就叫做 适配器模式
namespace hazb {
//栈借助其他容器存储数据
template<class T, class Container = vector<T>>
//使用模板,使之可以使用不同的容器以实现不同功能,默认使用vector容器
class stack {
public:
void push(const T &x) {
_con.push_back(x);
}
void pop() {
_con.pop_back();
}
T& top() {
return _con.back();
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
}
queue 同理
在上面的模拟实现中,Container 分别默认为 vector< T > 和 list< T >,但事实不然:
那么 deque 是什么:
deque(Double ended queue 双端队列)
大体结构示意图:
缺点:
为什么 deque 作为 queue 的默认容器优于 list:deque 具有更加连续的空间,能更好的配合 CPU 的高速缓存
● 优先级高的先出(默认大的数优先级高)
● 底层是个 堆(默认大堆)(把它当做堆来看就行)
● priority_queue< int, vector< int >, greater< int > > pq;
greater< int > 表示小堆(此处用到了 仿函数 )
即类的对象可以像函数一样使用:
struct Less {
bool operator()(int x, int y) { //重载()
return x < y;
}
};
int main() {
Less lessFunc;
cout << lessFunc(1, 2) << endl;//把lessFlunc这个对象像函数一样使用
return 0;
}
将类的对象当函数一样使用,可以解决许多问题,但这最开始是为了解决 函数指针 不好用的问题:
C语言中的 qsort(最后要传一个函数指针):
namespace hazb
{
template<class T>
//仿函数
struct less
{
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<class T>
struct greater
{
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
// 大堆
template<class T, class Container = vector<T>, class Comapre = less<T>>
class priority_queue
{
public:
void adjust_up(int child)
{
Comapre com;
int parent = (child - 1) / 2;
while (child > 0)
{
//if (_con[parent] < _con[child])
if (com(_con[parent], _con[child]))//★使用仿函数优化了上述写法 -- 此后控制大/小堆就无需像C语言一样还要修改自己写的堆里的<>了,直接修改传进来的仿函数就行
//if (Comapre()(_con[parent], _con[child]))//复习:这是什么写法 —— 匿名对象使用()重载,原来是有名对象
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void adjust_down(int parent)
{
size_t child = parent * 2 + 1;
while (child < _con.size())
{
Comapre com;
//if (child + 1 < _con.size()
// && _con[child] < _con[child + 1])
if (child + 1 < _con.size()
&& com(_con[child], _con[child + 1]))
{
++child;
}
//if (_con[parent] < _con[child])
if (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size() - 1);
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
const T& top()
{
return _con[0];
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
//测试用
void test_priority_queue()
{
priority_queue<int> pq;
pq.push(1);
pq.push(0);
pq.push(5);
pq.push(2);
pq.push(1);
pq.push(7);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
//测试:存储数据为自定义类型(日期类)时
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
bool operator<(const Date& d)const
{
return (_year < d._year) ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day);
}
bool operator>(const Date& d)const
{
return (_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
friend ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
private:
int _year;
int _month;
int _day;
};
class PDateLess
{
public:
bool operator()(const Date* p1, const Date* p2)
{
return *p1 < *p2;
}
};
class PDateGreater
{
public:
bool operator()(const Date* p1, const Date* p2)
{
return *p1 > *p2;
}
};
void test_priority_queue2()
{
//仿函数会调用重载的 < 与 >
priority_queue<Date, vector<Date>, greater<Date>> q1;
q1.push(Date(2018, 10, 29));
q1.push(Date(2018, 10, 30));
q1.push(Date(2018, 10, 28));
cout << q1.top() << endl;
priority_queue<Date*, vector<Date*>, PDateGreater> q2;
//存储数据为日期类的指针,如果仍用此前的 less 和 greater 仿函数,其中比较的就是地址大小 -- 不是我们想要的
//因而再重新设计一个仿函数
q2.push(new Date(2018, 10, 29));
q2.push(new Date(2018, 10, 30));
q2.push(new Date(2018, 10, 28));
cout << *(q2.top()) << endl;
}
}