+ -
当前位置:首页 → 问答吧 → 优先队列的问题,大家帮帮忙

优先队列的问题,大家帮帮忙

时间:2011-07-30

来源:互联网

初学数据结构,求给个优先队列的例子

作者: lykakaxi   发布时间: 2011-07-30

C/C++ code
#include "iostream"
#include"queue"
using namespace std;
struct node
{
    friend bool operator< (node n1, node n2)
    {
        return n1.priority < n2.priority;
    }
    int priority;
    int value;
};
//在该结构中,value为值,priority为优先级。
//通过自定义operator<操作符来比较元素中的优先级。

int main(void)
{
    const int len = 5;
    int i;
    priority_queue<node> qn;
    node b[len];
    b[0].priority = 6; b[0].value = 1; 
    b[1].priority = 9; b[1].value = 5; 
    b[2].priority = 2; b[2].value = 3; 
    b[3].priority = 8; b[3].value = 2; 
    b[4].priority = 1; b[4].value = 4; 

    for(i = 0; i < len; i++)
        qn.push(b[i]);
    cout<<"优先级"<<'\t'<<""<<endl;
    for(i = 0; i < len; i++)
    {
        cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
        qn.pop();
    }
    system("pause");
    return 0;
}

作者: Hackbuteer1   发布时间: 2011-07-30

http://www.cplusplus.com/reference/stl/priority_queue/push/

作者: dizuo   发布时间: 2011-07-30