STL 优先队列 用法

今天做题用到了优先队列 对它的用法还不是很熟悉 现在整理一下。

需要的库

#include<queue>
using namespace std;

不过我都用bits/stdc++.h...

定义

priority_queue<Type, Container, Functional>

Type是数据的类型 比如int啊char啊之类的

Container是容器类型默认是vector

Functional是比较的方式  比如greater<int>   less<int>   或者自己定义的比较函数

具体用法

基本用法1

priority_queue <int> q;

这是最基本的用法 不需要像定义一样传三个参数进去 只需要声明一个数据类型即可

需要注意的是 优先队列是默认从大到小排的!

基本用法2

//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

因为声明了比较的方式,这次必须要传三个参数进去了

需要注意的是:

  • greater<int>  和 > 之间必须要有一个空格,不然是右移运算符!
  • greater是升序排列,也就是从小到大排,不是我们想当然的greater就是从大到小!(所以这里只需要记住 greater是升序 up up~ less是降序down down~ 这样比较符合正常人的认知,也好记~)

进阶用法1(运算符重载)

方法1 使用 friend bool operator

typedef struct node
{
    int num;
    friend bool operator < (const node & a,const node & b)
    {
        return a.num < b.num ;
    }
}point;

 priority_queue<point>q;

这个方法是将运算符的重载在结构体的定义中完成,优先队列的的定义中就不需要传三个参数了 在这个小例子里看起来没什么用 不过解决复杂问题时,就需要采用结构体来设计数据结构 也就必须要告诉计算机,比较的方式。

需要注意的是:

  • 只能对小于号进行重载
  • 若想从小到大排 只需要将return语句的小于号改成大于号即可 和sort是正好反过来的!

方法2 使用 bool operator

typedef struct node
{
        int num;
        bool operator<(const node&b)const
        {
            return    num<b.num;
        }
}point;    

priority_queue<point>q;

和采用friend bool operator的方法1一样,只是写法略有不同 我不喜欢用 感觉乱乱的....

进阶用法2(重写仿函数)

struct node1
{
    int x;
};

struct node2
{
    bool operator() (node1 a, node1 b)
    {
        return a.x < b.x;
    }
};

priority_queue<node1, vector<node1>, node2> p;

重写仿函数这个用起来真麻烦呀....需要声明两个结构体 不喜欢用....

支持的操作

  • top 访问队头元素
  • empty 队列是否为空
  • size 返回队列内元素个数
  • push 插入元素到队尾 (并排序)
  • emplace 原地构造一个元素并插入队列
  • pop 弹出队头元素
  • swap 交换内容

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11247820.html

时间: 2024-07-31 04:00:07

STL 优先队列 用法的相关文章

poj3253 Fence Repair STL优先队列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3253 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map   list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [unordered_multimap]     contiguous storage double-ended queue LIFO FIFO 1st is greatest  

ZOJ2724_Windows Message Queue(STL/优先队列)

解题报告 题意: 看输入输出就很明白. 思路: 优先队列. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; struct node

优先队列用法

优先队列用法 在优先队列中,优先级高的元素先出队列.标准库默认使用元素类型的<操作符来确定它们之间的优先级关系.优先队列的第一种用法,也是最常用的用法: priority_queue<int> qi; 通过<操作符可知在整数中元素大的优先级高.故示例1中输出结果为:9 6 5 3 2 第二种方法:在示例1中,如果我们要把元素从小到大输出怎么办呢?这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数. priority_queue<int, vecto

STL+优先队列

1 #include<iostream> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 const int INF =100000000; 6 const int MAXN =1000; 7 const int MAXM =100000; 8 int m,n; 9 int first[MAXN],d[MAXN]; 10 int u[MAXM],v[MAXM],w[MAXM],next[MA

STL 优先队列的自定义比较函数与 sort() 等泛型算法的自定义比较函数的区别

前言 最近在刷算法题,常常需要自定义比较函数作为作为函数对象送入 stl 中,遇到了下面的问题: 泛型算法 sort() 的比较函数是这么写: //sort() 实现元素间关系为递增的比较函数 struct cmp{ bool operator () (const T& a, const T& b) const { return a.x < b.x; } }; //或者这样bool operator < (const T& a, const T& b) cons

UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500'th ugly number. Input Ther

STL优先队列详解

优先队列 优先队列是一种抽象数据类型(Abstract Date Type,ADT),行为和队列类似,但是先出队的元素不是先进队列的元素,而是队列中优先级最高的元素. STL的优先队列定义在头文件<queue>和 (队列一样),用"priority_queue<int>pq"来声明: 最基本的用法 定义:priority_queue<int>pq: 操作: pq.empty() 如果队列为空返回真 pq.pop() 删除对顶元素 pq.push()

C++STL——优先队列

一.相关定义 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队.这点类似于给队列里的元素进行了由大到小的顺序排序.元素的比较规则默认按元素值由大到小排序,可以重载"<"操作符来重新定义比较规则. 优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container不能用来实现queue,因为list的迭代器不是任意存