优先队列优先级队列是一种容器适配器,根据某种严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大的堆元素(优先级队列中位于顶部的元素)。
优先级队列是作为容器适配器实现的,容器适配器是使用特定对象的封装对象的类容器类作为其底层容器,提供一组特定的成员函数来访问其元素。
元素从特定容器的“后面”弹出,即优先级队列的顶部。
底层容器可以是任何标准容器类模板或其他特定设计的容器类。容器必须可通过随机访问迭代器访问,并支持以下操作:
- empty()
- size()
- front()
- push_back()
- pop_back()
仿函数实现比较大小
我们先初步认识一下仿函数,这里我们用来比较大小
int main()
{
less<int> lessfunc;
cout << lessfunc(1, 2) << endl;
cout << lessfunc(2, 1) << endl;
cout << lessfunc.operator()(2, 1) << endl;//实际调用时,我们调用的是函数的重载
return 0;
}
接下来,我们自定义实现两个优先级队列常用的仿函数
//仿函数/函数对象
template<class T>
class Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<class T>
class Great
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
优先级队列自定义实现
在调用优先级队列是,如果没有指定使用的仿函数的默认使用的为less
#pragma once
#include<vector>
//仿函数/函数对象
template<class T>
class Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<class T>
class Great
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
namespace test
{
template<class T, class Container = vector<T>, class Comapre = Less<T>>
class priority_queue
{
private:
void AdjustDown(size_t parent)
{
Comapre com;
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child],_con[child + 1]))
//if (child + 1 < _con.size() && _con[child]<_con[child + 1])
{
child++;
}
if (com(_con[parent],_con[child]))
{
swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void AdjustUp(size_t child)
{
Comapre com;
size_t parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
break;
}
}
public:
priority_queue(){}
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
{
while (first != last)
{
_con.push_back(*first);
first++;
}
//建堆
for (size_t i = (_con.size() - 1 - 1) / 2; i <= 0; --i)
{
AdjustDown(i);
}
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
const T& top()
{
return _con[0];
//return _con.front();
}
bool empty()
{
return _con.empty();
}
size_t size()
{
return _con.size();
}
private:
Container _con;
};
void test_priority_queue1()
{
priority_queue<int, vector<int>> pq;
pq.push(1);
pq.push(2);
pq.push(3);
pq.push(4);
while(!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
}
优先级队列之自定义类型
在使用自定义类型时,最好自己实现仿函数
#pragma once
#include<vector>
//仿函数/函数对象
template<class T>
class Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
//特化模板
template<class T>
class Less<T*>
{
public:
bool operator()(const T* x, const T* y)
{
return *x < *y;
}
};
template<class T>
class Great
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
namespace test
{
template<class T, class Container = vector<T>, class Comapre = Less<T>>
class priority_queue
{
private:
void AdjustDown(size_t parent)
{
Comapre com;
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child],_con[child + 1]))
//if (child + 1 < _con.size() && _con[child]<_con[child + 1])
{
child++;
}
if (com(_con[parent],_con[child]))
{
swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void AdjustUp(size_t child)
{
Comapre com;
size_t parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
break;
}
}
public:
priority_queue(){}
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
{
while (first != last)
{
_con.push_back(*first);
first++;
}
//建堆
for (size_t i = (_con.size() - 1 - 1) / 2; i <= 0; --i)
{
AdjustDown(i);
}
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
const T& top()
{
//return _con[0];
return _con.front();
}
bool empty()
{
return _con.empty();
}
size_t size()
{
return _con.size();
}
private:
Container _con;
};
//自定义类型
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);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
class LessPDate
{
public:
bool operator()(const Date* p1, const Date* p2)
{
return *p1 < *p2;
}
};
//struct LessPDate
//{
// bool operator()(const Date* p1, const Date* p2)
// {
// return *p1 < *p2;
// }
//};
void test_priority_queue2()
{
// 仿函数控制实现小堆
/* priority_queue<Date, vector<Date>, less<Date>> pq;
pq.push(Date(2023, 7, 20));
pq.push(Date(2023, 6, 20));
pq.push(Date(2023, 8, 20));
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;*/
//priority_queue<Date*, vector<Date*>, LessPDate> pq;
priority_queue<Date*>;
pq.push(new Date(2023, 7, 20));
pq.push(new Date(2023, 6, 20));
pq.push(new Date(2023, 8, 20));
while (!pq.empty())
{
cout << *pq.top() << " ";
pq.pop();
}
cout << endl;
}
}