hello~ 很高兴见到大家! 这次带来的是C++中关于priority_queue容器迭代器这部分的一些知识点,如果对你有所帮助的话,可否留下你宝贵的三连呢? 个 人 主 页: 默|笙


<堆介绍博客>

默认底层容器是vector的原因:因为它的下标访问速率更快。不支持list作为底层容器,因为list仅提供双向迭代器,而priority_queue需要的是随机迭代器。


关于priority_queue对象的创建:能够看到,priority_queue这个容器适配器有三个模板参数,依次是:T: 接收存储元素的类型,Container:接收底层容器的类型,Compare:接收比较器(一般传递仿函数类类型,之后会讲到)的类型。其中后面两个模板参数都有默认值,在进行实例化对象时可以不用传递参数。
//使用priority_queue时要包含头文件queue
priority_queue<int> pq1;关于Container:vector是默认容器,我们也可以根据需求来改变底层容器,比如deque。
priority_queue<int, deque<int>> pq2;关于Compare:默认值是less这个仿函数类型,因为它,priority_queue对象里大的数据的优先级更高,是大堆结构。我们可以传递另一个仿函数类型greater,这样是小堆结构,小的数据的优先级更高。比如下列代码:
priority_queue<int> pq1;
priority_queue<int, deque<int>> pq2;
//要传递第三个参数的话,第二个参数的值也不能漏掉
priority_queue<int, vector<int>, greater<int>> pq3;
pq1.push(1);
pq1.push(2);
pq1.push(3);
pq1.push(4);
pq3.push(1);
pq3.push(2);
pq3.push(3);
pq3.push(4);
cout << "less: ";
while (!pq1.empty())
{
cout << pq1.top() << " ";
pq1.pop();
}
cout << endl;
cout << "greater: ";
while (!pq3.empty())
{
cout << pq3.top() << " ";
pq3.pop();
}
它跟我们平常的思维习惯是反着来的,这一点要注意。其中的 < 与 > 是关于它的实现,可以参考下面的仿函数章节。

namespace mosheng//防止与std里的冲突
{
template<class T>
class less
{
public:
bool operator()(const T& a, const T& b)
{
return a < b;
}
};
template<class T>
class greater
{
public:
bool operator()(cosnt T& a, cosnt T& b)
{
return a > b;
}
};
}template<class T, class Compare = mosheng::less<T>>
void BubbleSort(T* a, int n, Compare com)
{
int exchange = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
//等价于com.operator()(a[j + 1], a[j])
if (com(a[j + 1], a[j]))
{
exchange = 1;
swap(a[j], a[j + 1]);
}
}
if (exchange == 0)
{
break;
}
}
}
int main()
{
int arr[] = { 1, 5, 12, 2, 1, 3, 77 };
BubbleSort(arr, 7, mosheng::less<int>());
cout << "less: ";
for (auto e : arr)
{
cout << e << " ";
}
cout << endl;
cout << "greater: ";
BubbleSort(arr, 7, mosheng::greater<int>());
for (auto e : arr)
{
cout << e << " ";
}
cout << endl;
return 0;
}就有:

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;
}
//构建仿函数
struct PDateLess
{
bool operator()(const Date* p1, const Date* p2)
{
return *p1 < *p2;
}
};
int main()
{
priority_queue<Date*, vector<Date*>> q1;
q1.push(new Date(2025, 7, 20));
q1.push(new Date(2025, 7, 21));
q1.push(new Date(2025, 7, 22));
cout << "原始:";
while (!q1.empty())
{
cout << *q1.top() << " ";
q1.pop();
}
cout << endl;
cout << "构建的PDteLess:";
priority_queue<Date*, vector<Date*>, PDateLess> q2;
q2.push(new Date(2025, 7, 20));
q2.push(new Date(2025, 7, 21));
q2.push(new Date(2025, 7, 22));
while (!q2.empty())
{
cout << *q2.top() << " ";
q2.pop();
}
cout << endl;
return 0;
}

struct op1
{
bool operator()(int x)
{
return x % 2 == 0;
}
};
int main()
{
int a[] = { 1, 3, 5, 8, 2 };
//找第一个偶数
auto x = find_if(a, a + 5, op1());
cout << *x << endl;
return 0;
}小总结:
一般在两种时候我们自己构造仿函数:
priority_queue的模拟实现离不开堆排序里的向上调正算法和向下调整算法,可以看这篇博客->堆排序
template<class T>
class Less
{
public:
bool operator()(const T& a, const T& b)
{
return a < b;
}
};
template<class T>
class Greater
{
public:
bool operator()(const T& a, const T& b)
{
return a > b;
}
};
namespace mosheng
{
template<class T, class Container = vector<T>, class Compare = Less<T>>
class priority_queue
{
public:
priority_queue() = default;//强制生成默认构造函数
//range构造函数
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
: _con(first, last)
{
//向下调整建堆
size_t parent = (_con.size() - 1 - 1) / 2;
while (parent >= 0)
{
size_t child = parent * 2 + 1;
if ((child + 1 < _con.size()) && (_con[child] < _con[child + 1]))
{
child = child + 1;
}
if (com(_con[parent], _con[child]))
{
std::swap(_con[child], _con[parent]);
}
parent--;
}
}
//插入
void push(const T& x)
{
_con.push_back(x);
//向上调整算法
AdjustUp();
}
//取堆顶数据
T& top()
{
return _con[0];
}
const T& top()const
{
return _con[0];
}
//移除
void pop()
{
//先交换首尾数据再向下调整数据
std::swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
//向下调整数据
size_t parent = 0;
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if ((child + 1 < _con.size()) && (_con[child] < _con[child + 1]))
{
child = child + 1;
}
if (com(_con[parent], _con[child]))
{
std::swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
//返回大小
size_t size()
{
return _con.size();
}
bool empty()const
{
return _con.empty();
}
private:
void AdjustUp()
{
size_t child = _con.size() - 1;
size_t parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent], _con[child]))
{
std::swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
private:
Container _con;
Compare com;
};
}今天的分享就到此结束啦,如果对读者朋友们有所帮助的话,可否留下宝贵的三连呢~~ 如果可以, 那就让我们共同努力, 一起走下去!