【发布时间】:2020-11-28 18:30:17
【问题描述】:
我正在为优先级队列实现自定义比较功能,并看到 sort 和 优先级队列 STL 的比较功能之间的区别。
假设,我们有几个点(x, y),对于这些点,我们有一个用户定义的类。
class Point
{
int x;
int y;
public:
Point(int _x, int _y)
{
x = _x;
y = _y;
}
int getX() const { return x; }
int getY() const { return y; }
};
还有自定义比较功能:
class myComparator
{
public:
int operator() (const Point& p1, const Point& p2)
{
return p1.getX() > p2.getX();
}
};
现在,如果我们将比较函数传递给 sort STL,那么它将按照 x 坐标的降序对点进行排序。
int main ()
{
vector<Point>v;
v.push_back(Point(10, 1));
v.push_back(Point(1, 5));
v.push_back(Point(2, 1));
sort(v.begin(), v.end(), myComparator());
for(auto p: v)
{
cout << "(" << p.getX() << ", " << p.getY() << ")"<<endl;
}
return 0;
}
输出:
(10, 1)
(2, 1)
(1, 5)
现在,如果我对优先级队列应用相同的比较函数,它将按降序排列。基本上,它作为 min heap 工作。
int main ()
{
priority_queue <Point, vector<Point>, myComparator > pq;
pq.push(Point(10, 2));
pq.push(Point(2, 1));
pq.push(Point(1, 5));
while (pq.empty() == false)
{
Point p = pq.top();
cout << "(" << p.getX() << ", " << p.getY() << ")";
cout << endl;
pq.pop();
}
return 0;
}
输出:
(1, 5)
(2, 1)
(10, 2)
我期待点将像排序函数一样排序,就像我在下面编写的自定义比较函数中一样:
p1.getX() > p2.getX()
为什么比较函数在优先级队列中的工作方式不同?
【问题讨论】:
-
"请注意,Compare 参数的定义是,如果它的第一个参数在弱排序的第二个参数之前,它会返回 true。但是因为优先级队列首先输出最大的元素,所以元素“come before”实际上是最后输出的。也就是说,队列的前面包含根据 Compare 强加的弱排序的“last”元素。“ en.cppreference.com/w/cpp/container/priority_queue
标签: c++ stl priority-queue