优先队列(priority_queue)

定义:

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。通常采用堆数据结构来实现。

STL中的优先队列priority_queue,包含在头文件”queue”中,可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。

定义:priority_queue<Type, Container, Functional>
Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆

一般是:

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

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

1.优先队列的常用操作

 q.empty()          如果队列为空,则返回true,否则返回false
 
 q.size()           返回队列中元素的个数
 
 q.pop()            删除队首元素,但不返回其值
 
 q.top()            返回具有最高优先级的元素值,但不删除该元素
 
 q.push(item)       在基于优先级的适当位置插入新元素

其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以速度很快;另外,在优先队列中,元素可以具有相同的优先权。

2.优先队列常用定义和重载运算符方法

①默认优先级:

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

③传入比较结构体,自定义优先级。

#include <queue>
using namespace std;
 
struct cmp{
    bool operator ()(int a,int b){    //通过传入不同类型来定义不同类型优先级
        return a>b;    //最小值优先
    }
};
/**
struct cmp{
    bool operator ()(int a,int b){
        return a<b;    //最大值优先
    }
};
**/
 
 
priority_queue<int, vector<int>, cmp > q

④自定义数据结构,自定义优先级。

#include <queue>
using namespace std;
 
struct node {
    int priority;
    int value;
    friend bool operator < (const node &a, const node &b) 
        return a.priority < b.priority;
}

priority_queue<node> q;
//优先队列的研究2:数据结构的优先队列方法
#include<iostream>
#include<queue>
 
using namespace std;
struct time
{
	int hour;
	int minute;
	int second;
	
	//第一种重载方法,结构体内重载,当如此重载时,优先队列的定义方法:priority_queue<time> q;
	friend bool operator < (time t1,time t2)	//此处运算符重载
	{
		if(t1.hour==t2.hour)
		{
			if(t1.minute==t2.minute)
			{
				return t1.second<t2.second;
			}
			else
				return t1.minute<t2.minute;
		}
		else
			return t1.hour<t2.hour;
	 } 
	 //最终的效果会是一个大根堆 
};
 
/*第二种重载方法,结构体外重载比较函数 
struct cmp{
	bool operator()(time t1,time t2)
	{
		if(t1.hour==t2.hour)
		{
			if(t1.minute==t2.minute)
			{
				return t1.second<t2.second;
			}
			else
				return t1.minute<t2.minute;
		}
		else
			return t1.hour<t2.hour;
		
	}
};
*/
 
int main()
{
	priority_queue<time> q;
	//priority_queue<time,vector<time>,cmp > q;		//当使用类外重载比较函数的时候优先队列要如此定义 
	int n,i;
	time ti;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>ti.hour>>ti.minute>>ti.second;
		q.push(ti);
	}
	while(!q.empty())
	{
		ti=q.top();
		cout<<ti.hour<<":"<<ti.minute<<":"<<ti.second<<endl;
		q.pop();
	}
	return 0;
 } 

代码示例

#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
 
using namespace std;
struct cmp1
{
    bool operator()(int x, int y)
    {
        return x > y;//小的优先级高
    }
};
 
struct node
{
    int x;
    int y;
    friend bool operator <(const node &a, const node &b)
    {
        return a.x > b.x;//小的优先级高
    }
};
priority_queue<int, vector<int>,cmp1>q2;
priority_queue<node>q3;
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++)
    {
            node a;
            cin>>a.x>>a.y;
            q3.push(a);
     }
    cout<<endl;
    while(!q3.empty())
    {
        cout<<q3.top().x<<"  "<<q3.top().y<<endl;
        q3.pop();
    }
    return 0;
}

1.基本例子:

#include<iostream>
#include <queue>
using namespace std;
int main() 
{
    //对于基础类型 默认是大根堆
    priority_queue<int> a; 
    //等同于 priority_queue<int, vector<int>, less<int> > a;
    
    //             这里一定要有空格,不然成了右移运算符↓
    priority_queue<int, vector<int>, greater<int> > c;  //这样就是小根堆
    priority_queue<string> b;

    for (int i = 0; i < 5; i++) 
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) 
    {
        cout << a.top() << ' ';
        a.pop();
    } 
    cout << endl;

    while (!c.empty()) 
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) 
    {
        cout << b.top() << ' ';
        b.pop();
    } 
    cout << endl;
    return 0;
}

输出

4 3 2 1 0
0 1 2 3 4
cbd abcd abc

2.pari的比较,先比较第一个元素,第一个相等比较第二个

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() 
{
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
}

3.对于自定义类型

#include <iostream>
#include <queue>
using namespace std;

//方法1
struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};

//方法2
struct tmp2 //重写仿函数
{
    bool operator() (tmp1 a, tmp1 b) 
    {
        return a.x < b.x; //大顶堆
    }
};

int main() 
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) 
    {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(c);
    f.push(b);
    f.push(a);
    while (!f.empty()) 
    {
        cout << f.top().x << '\n';
        f.pop();
    }
}

3
2
1

3
2
1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值