stack理解为栈就可以了
栈的话先进后出
我觉得这个真没啥说的
stack<int> s;
压栈
s.push(x);
出栈
s.pop(s);
还有判空啥的
queue理解为队列
判空啊插入取出就不说了
说说优先级队列
priority_queue
优先级队列创建出来是按照堆排序来的,默认创建出来是大堆
vector<int> v{ 3, 1, 0, 9, 5, 8, 6, 2, 4, 7 };
priority_queue<int> q2(v.begin(),v.end());
创建好的优先级队列为
9 7 8 4 5 0 6 2 1 3
这是按照大堆创建的
如果要创建小堆
priority_queue<int,vector<int>,greater<int>> q3(v.begin(), v.end());
创建出来就是小堆,这会很容易排序
如果想要在优先级队列中放置自定义类型,比如我们熟悉的日期类
class Date {
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
}
private:
int _year;
int _month;
int _day;
};
想象一下,如果实例化了是三个对象
Date d1(2088, 6, 27);
Date d2(2088, 6, 28);
Date d3(2032, 6, 26);
在优先级队列中怎么比较
是按照年份比较还是月份?
其实在优先级队列中现在是按照地址大小排列的
那么要是想要正常按照日期排列怎么办呢?
首先在类里提供> <的重载
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);
}
然后创建就行了