如何创建优先队列以升序存储对?
我需要创建一个队列,用于按第一个值升序存储整数对。
假设我有以下队列:
0 10
0 10
1 10
2 10
30 10
如果我尝试使用这些值创建一个优先级队列,它只会按降序存储这些对,从 30 开始一直到 0。
有没有一种方法可以对队列进行排序或只是在声明中设置顺序?
我正在尝试做:
priority_queue<pair<int, int>> queue;
for(int i=0; i<n; i++){
cin>>t>>d;
queue.push(make_pair(t, d));
}
I need to create a queue that stores pairs of integers in ascending order by their first value.
Say I have the following queue:
0 10
0 10
1 10
2 10
30 10
If I try to create a priority queue with these values, it's just going to store the pairs by descending order, starting with 30 all the way to 0.
Is there a way to sort the queue or to just set the order in the declaration?
I'm trying to do:
priority_queue<pair<int, int>> queue;
for(int i=0; i<n; i++){
cin>>t>>d;
queue.push(make_pair(t, d));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 priortition_queue ,最大的元素是在队列的前面。
您可以将
std ::更大的&lt; pair&lt; int,int&gt;&gt;
用作自定义比较器或您自己的比较器,也可以使用自定义订购。这将使最小的元素放在队列的前面。For priority_queue, the largest element is at the front of the queue.
You could use
std::greater<pair<int,int>>
as custom comparator or your own comparator as well to have a custom ordering. This will put the smallest element at the front of the queue.