简体   繁体   English

优先队列比较器[C++]

[英]Comparator for Priority Queue[C++]

I am trying to understand what my comparator should return for a Priority Queue of vectors where I want a min-heap based on third element.我试图了解我的比较器应该为向量的优先级队列返回什么,我想要一个基于第三个元素的最小堆。

On searching, I found out that the comparator looks like:在搜索中,我发现比较器看起来像:

bool operator()(vector<int>a, vector<int> b)
{
return a[2]>b[2];
}

Why not a[2]<b[2] ?为什么不a[2]<b[2]

A max heap is a vector or binary tree where the data contained in each node is greater than or equal to that of its children, so the leaves have the smallest values in the queue.最大堆是一个向量或二叉树,其中每个节点中包含的数据大于或等于其子节点的数据,因此叶子在队列中具有最小值。 Internally, the priority queue is represented as a max heap and sorted so that nodes with the largest value can be accessed first.在内部,优先级队列表示为最大堆并进行排序,以便可以首先访问具有最大值的节点。 If you wanted to sort elements a[2]<b[2] , you can overwrite the comparator.如果要对元素a[2]<b[2]进行排序,可以覆盖比较器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM