优先队列用法

在优先队列中,优先级高的元素先出队列。

标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。

优先队列的第一种用法:

也是最常用的用法

priority_queue<int> qi;

通过<操作符可知在整数中元素大的优先级高。

故示例1中输出结果为:9 6 5 3 2

第二种用法:

在示例1中,如果我们要把元素从小到大输出怎么办呢?

这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

priority_queue<int, vector<int>, greater<int> >qi2; //从小到大的优先级队列

可将greater改为less即为从大到小

其中 第一个参数为容器类型。 第二个参数为比较函数。

故示例2中输出结果为:2 3 5 6 9

第三种用法: 自定义优先级。

struct node

{

   friend bool operator< (node n1, node n2)

{

   return n1.priority < n2.priority; //"<"为从大到小排列">"为从小打到排列

}

int priority;

int value;

};

在该结构中,value为值,priority为优先级。

通过自定义operator<操作符来比较元素中的优先级。

在示例3中输出结果为:

优先级  值

9          5

8          2

6          1

2          3

1          4

但如果结构定义如下:

struct node

{

   friend bool operator> (node n1, node n2)

{

return n1.priority > n2.priority;

}

int priority;

int value;

};

则会编译不过(G++编译器)

因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。

而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。

//代码清单

 #include<iostream>
#include<functional>
#include<queue>
using namespace std;
struct node
{
    friend bool operator< (node n1, node n2)
    {
        return n1.priority < n2.priority;
    }
    int priority;
    int value;
};
int main()
{
    const int len = ;
    int i;
    int a[len] = {,,,,};
    //示例1
    priority_queue<int> qi;
    for(i = ; i < len; i++)
        qi.push(a[i]);
    for(i = ; i < len; i++)
    {
        cout<<qi.top()<<" ";
        qi.pop();
    }
    cout<<endl;
    //示例2
    priority_queue<int, vector<int>, greater<int> >qi2;
    for(i = ; i < len; i++)
        qi2.push(a[i]);
    for(i = ; i < len; i++)
    {
        cout<<qi2.top()<<" ";
        qi2.pop();
    }
    cout<<endl;
    //示例3
    priority_queue<node> qn;
    node b[len];
    b[].priority = ; b[].value = ; 
    b[].priority = ; b[].value = ; 
    b[].priority = ; b[].value = ; 
    b[].priority = ; b[].value = ; 
    b[].priority = ; b[].value = ;      for(i = ; i < len; i++)
        qn.push(b[i]);
    cout<<"优先级"<<'\t'<<"值"<<endl;
    for(i = ; i < len; i++)
    {
        cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
        qn.pop();
    }
    return ;
}

STL之优先队列(1)的更多相关文章

  1. STL之优先队列

    STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...

  2. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  3. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  4. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  5. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  6. W - stl 的 优先队列 Ⅲ

    Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...

  7. V - stl 的 优先队列 Ⅱ

    Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...

  8. hdu 4393 Throw nails(STL之优先队列)

    Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...

  9. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. HDU 4597 Play Game

    题目链接 什么都不想说,最近状态暴跌.. #include <cstdio> #include <cstring> #include <iostream> usin ...

  2. SQL Server2008清空日志文件

    USE[master] GO ALTER DATABASE mydbname SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE mydbname S ...

  3. python基础学习——第二天

    一.python种类 1.1 Cpython python官方版本,使用c语言实现,运行机制:先编译,py(源码文件)->pyc(字节码文件),最终执行时先将字节码转换成机器码,然后交给cpu执 ...

  4. Eclipse安装nodeclipse插件

    1. Start Eclipse, then select Help > Install New Software... 2. Enter the update site URL into th ...

  5. 改进iOS客户端的升级提醒功能

    改进iOS客户端的升级提醒功能 功能设计 先申明一下,我是码农,不是一个产品经理,但我觉得现有市面上的很多 App,设计的 "升级提示功能" 都不太友好.在此分享一下我的想法,欢迎 ...

  6. Struts2中重定向和请求转发配置

    struts2中默认跳转为dispatcher请求转发 只能往jsp转发,跳转action报404 重定向 设置为redirect ,可以是jsp也可以是action <!--同一个包下的act ...

  7. Spring中的单例一二

    Spring框架很好的帮助我们创建和管理dao.bean.service.action等对象, 但是它创建的对象是单例呢还是多例,又有哪些区别以及为什么 1.在Spring中默认创建的是单例模式,简单 ...

  8. 下载包含src,tgz,zip的文件

    下载哪一个文件? 含src的是源码文件,含tgz和zip的分别是linux和windows系统下jar包(java文件包) asc,md5,sha1是三种加密文件,可用于判断文件是否被修改. Ente ...

  9. Struts 2.x No result defined for action 异常

      这是我跑struts2的第一个例子,跑的也够郁闷的,这个问题烦了我几个钟... 2011-5-10 10:10:17 com.opensymphony.xwork2.util.logging.co ...

  10. ul+li标签制作表格

    table标签制作表格代码繁琐,且不方便后期代码维护. li标签加上css的浮动样式可以制作多种样式的表格. 代码如下: <ul id="ttttt" style=" ...