C++STL——优先队列

一、相关定义

优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素。但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队。这点类似于给队列里的元素进行了由大到小的顺序排序。元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。

优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container不能用来实现queue,因为list的迭代器不是任意存取iterator,而pop中用到堆排序时是要求randomaccess iterator 的!):
priority_queue<vector<int>, less<int> > pq1;     // 使用递增less<int>函数对象排序
priority_queue<deque<int>, greater<int> > pq2;   // 使用递减greater<int>函数对象排序
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。

二、priority_queue

基本操作:

empty()      如果队列为空,则返回真

pop()    删除对顶元素,删除第一个元素

push()        加入一个元素

size()      返回优先队列中拥有的元素个数

top()     返回优先队列对顶元素,返回优先队列中有最高优先级的元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int> q;                 //通过操作,按照元素从大到小的顺序出队

priority_queue<int,vector<int>, greater<int> > q;    //通过操作,按照元素从小到大的顺序出队

2、自定义优先级:

struct cmp {

  operator bool ()(int x, int y)

  {

     return x > y;   // x小的优先级高       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高

  }

};

priority_queue<int, vector<int>, cmp> q;    //定义方法

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

3、结构体声明方式:

struct node {

  int x, y;

  friend bool operator < (node a, node b)

  {

    return a.x > b.x;    //结构体中,x小的优先级高

  }

};

priority_queue<node>q;   //定义方法

//在该结构中,y为值, x为优先级。

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

//在重载”<”时,最好不要重载”>”,可能会发生编译错误

三、代码实现

优先队列,其构造及具体实现我们可以先不用深究,我们现在只需要了解其特性,及在做题中的用法。以一个例子来解释吧(呃,写完才发现,这个代码包函了几乎所有我们要用到的用法,仔细看看吧):
/*优先队列的基本使用    2017/8/1    xzxl*/
#include<stdio.h>
#include<functional>
#include<queue>
#include<vector>
using namespace std;
//定义结构,使用运算符重载,自定义优先级1
struct cmp1{
    bool operator ()(int &a,int &b){
        return a>b;//最小值优先
    }
};
struct cmp2{
    bool operator ()(int &a,int &b){
        return a<b;//最大值优先
    }
};
//定义结构,使用运算符重载,自定义优先级2
struct number1{
    int x;
    bool operator < (const number1 &a) const {
        return x>a.x;//最小值优先
    }
};
struct number2{
    int x;
    bool operator < (const number2 &a) const {
        return x<a.x;//最大值优先
    }
};
int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};  

int main()
{   priority_queue<int>que;//采用默认优先级构造队列  

    priority_queue<int,vector<int>,cmp1>que1;//最小值优先
    priority_queue<int,vector<int>,cmp2>que2;//最大值优先  

    priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
                                                      //这是右移运算符,所以这里用空格号隔开
    priority_queue<int,vector<int>,less<int> >que4;////最大值优先  

    priority_queue<number1>que5;
    priority_queue<number2>que6;  

    int i;
    for(i=0;a[i];i++){
        que.push(a[i]);
        que1.push(a[i]);
        que2.push(a[i]);
        que3.push(a[i]);
        que4.push(a[i]);
    }
    for(i=0;num1[i].x;i++)
        que5.push(num1[i]);
    for(i=0;num2[i].x;i++)
        que6.push(num2[i]);  

    printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
    printf("Queue 0:\n");
    while(!que.empty()){
        printf("%3d",que.top());
        que.pop();
    }
    puts("");
    puts("");  

    printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
    printf("Queue 1:\n");
    while(!que1.empty()){
        printf("%3d",que1.top());
        que1.pop();
    }
    puts("");
    printf("Queue 2:\n");
    while(!que2.empty()){
        printf("%3d",que2.top());
        que2.pop();
    }
    puts("");
    puts("");
    printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
    printf("Queue 3:\n");
    while(!que3.empty()){
        printf("%3d",que3.top());
        que3.pop();
    }
    puts("");
    printf("Queue 4:\n");
    while(!que4.empty()){
        printf("%3d",que4.top());
        que4.pop();
    }
    puts("");
    puts("");
    printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
    printf("Queue 5:\n");
    while(!que5.empty()){
        printf("%3d",que5.top());
        que5.pop();
    }
    puts("");
    printf("Queue 6:\n");
    while(!que6.empty()){
        printf("%3d",que6.top());
        que6.pop();
    }
    puts("");
    return 0;
}
/*
运行结果 :
采用默认优先关系:
(priority_queue<int>que;)
Queue 0:
83 72 56 47 36 22 14 10  7  3 

采用结构体自定义优先级方式一:
(priority_queue<int,vector<int>,cmp>que;)
Queue 1:
 7 10 14 22 36 47 56 72 83 91
Queue 2:
83 72 56 47 36 22 14 10  7  3 

采用头文件"functional"内定义优先级:
(priority_queue<int,vector<int>,greater<int>/less<int> >que;)
Queue 3:
 7 10 14 22 36 47 56 72 83 91
Queue 4:
83 72 56 47 36 22 14 10  7  3 

采用结构体自定义优先级方式二:
(priority_queue<number>que)
Queue 5:
 7 10 14 22 36 47 56 72 83 91
Queue 6:
83 72 56 47 36 22 14 10  7  3
*/

  

时间: 2024-08-05 15:20:53

C++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

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

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优先队列的使用

STL中有一个优先队列的容器可以使用. [头文件] queue 队列容器 vector 向量容器 [操作] 优先级队列支持的操作 q.empty()         如果队列为空,则返回true,否则返回false q.size()            返回队列中元素的个数 q.pop()             删除队首元素,但不返回其值 q.top()             返回具有最高优先级的元素值,但不删除该元素 q.push(item)     在基于优先级的适当位置插入新元素 1

STL优先队列详解

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

stl 优先队列(堆)

[模板]堆 题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: 第一行包含一个整数N,表示操作的个数 接下来N行,每行包含1个或2个正整数,表示三种操作,格式如下: 操作1: 1 x 操作2: 2 操作3: 3 输出格式: 包含若干行正整数,每行依次对应一个操作2的结果. 输入输出样例 输入样例#1: 5 1 2 1 5 2 3 2 输出样例#1:

c++ STL 优先队列

操作1: 新建一个优先队列: ps:该操作需要包含头文件queue priority_queue < int , vector<int> , greeter <int> > t1; priority_queue < int , vector<int> , less <int> > t2; 解释: 接下来对解释一下新建时的三个参数: 其中第一个参数为优先队列的数据类型,可以是int,double之类的 其中第二个参数是指用什么容器来储存