美文网首页
优先队列

优先队列

作者: 摇摆苏丹 | 来源:发表于2023-07-04 09:24 被阅读0次
struct Point
{
    double x;
    double y;
    double z;
};

int main()
{
    auto comparator = [](Point p1, Point p2)
    {
        return p1.z > p2.z;
    };

    priority_queue<Point, vector<Point>, decltype(comparator)> pq(comparator);
    pq.push({ 1,1,4 });
    pq.push({ 1,1,2 });
    pq.push({ 1,1,5 });
    pq.push({ 1,1,1 });
    pq.push({ 1,1,9 });
    pq.push({ 1,1,6 });
    pq.push({ 1,1,4 });
    pq.push({ 1,1,3 });

    while (!pq.empty())
    {
        Point p = pq.top();
        pq.pop();
        printf("%.4f %.4f %.4f\n", p.x, p.y, p.z);
    }
    return 0;
}
struct Point
{
    double x;
    double y;
    double z;

    friend bool operator < (Point p1, Point p2)
    {
        return p1.z > p2.z;
    };
};

int main()
{
    priority_queue<Point> pq;
    pq.push({ 1,1,4 });
    pq.push({ 1,1,2 });
    pq.push({ 1,1,5 });
    pq.push({ 1,1,1 });
    pq.push({ 1,1,9 });
    pq.push({ 1,1,6 });
    pq.push({ 1,1,4 });
    pq.push({ 1,1,3 });

    while (!pq.empty())
    {
        Point p = pq.top();
        pq.pop();
        printf("%.4f %.4f %.4f\n", p.x, p.y, p.z);
    }
    return 0;
}

相关文章

网友评论

      本文标题:优先队列

      本文链接:https://www.haomeiwen.com/subject/jhncudtx.html