优先队列用法

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

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

优先队列的第一种用法:

也是最常用的用法

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. Big Chocolate

    Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...

  2. js判断三个数字中的最大值

    <script> //方法一: function maxOf3(c,d,e){ return (((c>d)?c:d)>e ? ((c>d)?c:d) : e); } c ...

  3. text-indent:-9999px 字体隐藏问题

    为什么要字体隐藏? 通常为了传达更好的视觉效果,我们常用图片替代掉字体.但是为了html语义化,常常要给内容模块加上一些标题来让页面更有意义,在抛开css裸奔的情况下也能很顺利的汲取到页面信息.为此我 ...

  4. border-radius

  5. sql 日期格式化

    sql语句中 经常操作操作datetime类型数据.今天在写一个存储过程的时候需要将 一个datetime的值的 日期部分提取出来.网上有许多这方面的介绍. 主要方法还是通过日期格式的转换来获取.如下 ...

  6. 测试的程序 test.php,保存放IIS的根目录下

    IIS+PHP的配置的方法,试过之后很多都不能达到效果.于是总结了大部分的文章后就得出了这样的方法 一.下载必须的程序:(1) 先到PHP的官方网站下载一个PHP(本文就以PHP 4.4.2为例).网 ...

  7. 配置perl-cgi的运行环境,由于Active Perl安装在d:\perl

    Apache 1.3.22 for Win32+PHP 4.0.6+Active Perl 5.006001+Zend Optimizer v1.1.0+mod_gzip 1.3.19.1a+MySQ ...

  8. 创建一个LinkedList,然后在其中插入多个值,确保每个值都插入到List中间(偶数中间两个数之一,奇数在正中间)

    这是Thinking in java 中的一道题,下面是我的解决方案: package test; import java.util.LinkedList; import java.util.List ...

  9. Git subtree和Git submodule

    git submodule允许其他的仓库指定以一个commit嵌入仓库的子目录. git subtree替代git submodule命令,合并子仓库到项目中的子目录.不用像submodule那样每次 ...

  10. .Net using,string.Empty初探

    前两天够哦年公司培训,讲了编码优化.现在初步总结下:(有些不大确定的就不讲了) 多次字符串拼接(特别是循环内),宜用stringBuilder.Append()方法,少用字符串+,至于string.F ...