您的位置:首页 > 其它

结构体的优先队列

2015-09-08 11:24 435 查看
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <algorithm>

#include <math.h>

#include <queue>

#include <stack>

#include <map>

#define INF 123123123

#define MAX_INDEX 100005

using namespace std;

struct node

{

    double a;

    string name;

    bool operator > (const node &other) const

    {

        if ((a > other.a))

        {

            return true;

        }

        return false;

    }

    bool operator < (const node &other) const

    {

        if ((a < other.a))

        {

            return true;

        }

        return false;

    }

};

int main()

{

    priority_queue<node,vector<node>,greater<node> >q;

//注意:这里使用了greater的话,那node结构体里必须重载了>比较符!

//如果没有使用greater,那就是小根堆,那node里必须重载<比较符!

    node temp;

    temp.name = "s3";

    temp.a = 92.78;

    q.push(temp);

    temp.name = "s2";

    temp.a = 91.66;

    q.push(temp);

    temp.name = "s1";

    temp.a = 91.78;

    q.push(temp);

    temp.name = "s4";

    temp.a = 91.41;

    q.push(temp);

    while(!q.empty())

    {

        node temp1 = q.top();

        q.pop();

        cout << temp1.name << " = " << temp1.a << endl;

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: