优先队列

本文介绍了一个使用 C++ 实现的优先队列示例,通过自定义比较器 cmp 来控制元素的排序方式。代码展示了如何声明和初始化优先队列,并通过 cin 输入一系列整数将其插入到队列中,最后按优先级顺序输出这些元素。
 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 using namespace std;
 5 
 6 struct cmp
 7 {
 8     bool operator()( const int &a, const int &b )
 9     {
10         return a < b;
11     }
12 };
13 
14 int main()
15 {
16     //priority_queue< int, vector<int>, greater<int> > q;
17     //priority_queue<int> q;
18     //priority_queue< int, vector<int>, less<int> > q;
19     priority_queue< int, vector<int>, cmp > q;
20     int n;
21     cin >> n;
22     while(n--)
23     {
24         int cmp;
25         cin >> cmp;
26         q.push(cmp);
27     }
28     while(!q.empty())
29     {
30         cout << q.top() << endl;
31         q.pop();
32     }
33     return 0;
34 }
35 //4006

 

转载于:https://www.cnblogs.com/jxust-jiege666/p/6523089.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值