C++STL队列以及优先队列小结

本文转载自该两处链接:优先队列 priority_queue 详解队列与优先队列的总结
其他相关博客:
C++ STL 队列的使用(普通队列,双端队列,优先队列)
C++STL——优先队列

- 队列

一、队列的定义:

是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)

c++队列queue模板类的定义在头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为 deque类型。

C++队列Queue类成员函数有:

  1. back() 返回最后一个元素
  2. empty() 如果队列空则返回真 ///空则返回1,否则返回0
  3. front() 返回第一个元素
  4. pop() 删除第一个元素
  5. push() 在末尾加入一个元素
  6. size() 返回队列中元素的个数

二、queue的头文件

queue需要以下两个头文件:

#include< queue >
using namespace std;

三、queue的声明与构造函数

queue声明的基本结构是这样的:

queue<数据结构>队列名;

比如:

queue< int > q;
queue< double > d;
queue< node > n;//node是一个结构体

四、queue的基本操作

首先,我们queue< int > q;

q.size();  //返回q里元素个数

q.empty();  //返回q是否为空,空则返回1,否则返回0

q.push(k);  //在q的末尾插入k

q.pop();  //删掉q的第一个元素

q.front();  //返回q的第一个元素

q.back();  //返回q的末尾元素

代码应用例子(例如0-5,共5个元素,我们对其进行入队出队操作):

#include<queue>
#include<cstdio>
using namespace std;
int main()
{
    queue <int> myQ;
    printf("现在队列中元素个数%d\n\n",myQ.size());
    for(int i =0; i<5 ; i++)
    {
        myQ.push(i);//入队
    }
    for(int i=0; !myQ.empty(); i++)
    {
        printf("队列剩余元素个数为:%d\n\n",myQ.size());
        printf("队列的第一个元素为:%d   队列最后一个元素为:%d\n\n",myQ.front(),myQ.back());
        myQ.pop();//出队
    }
    myQ.push(8);//在队列的最后一位添加一个元素
    printf("队列压入的元素为:%d\n",myQ.back());
    return 0;
}

- 优先队列

一、定义:

C++优先队列类似队列,但是在这个数据结构中的元素按照一定顺序排列。(优先队列是一种功能强大的队列,它的功能强大在哪里呢?四个字:自动排序。)

成员函数有:(优先队列没有back()操作!!!!!

  1. empty() 如果优先队列为空,则返回真
  2. pop() 删除第一个元素
  3. push() 加入一个元素
  4. size() 返回优先队列中拥有的元素的个数
  5. top() 返回优先队列中有最高优先级的元素

二、优先队列的头文件和声明

#include< queue >
using namespace std;

using namespace std; 这句话,代表,使用一个叫做“std”的namespace,namespace里面封存了一系列东西,比方说奇异的数据结构和奇异的函数。当打开了namespace以后,就跟打开了头文件的本质是一样的,都是可以直接用它里面封存的函数。
不同之处在什么地方?就是不开namespace的使用,在你想用的(以std为例)函数面前加上“std::”即可。
例如,std::sort(a+1,a+1+N); 之类的。

其次,一个优先队列声明的基本格式是:
priority_queue<结构类型> 队列名;
比如:

priority_queue <int> i;
priority_queue <double> d;

不过,我们最为常用的是这几种:

priority_queue <node> q;
//node是一个结构体
//结构体里重载了‘<’小于符号
priority_queue <int,vector<int>,greater<int> > q;
//不需要#include<vector>头文件
//注意后面两个“>”不要写在一起,“>>”是右移运算符
priority_queue <int,vector<int>,less<int> >q;

三、优先队列的基本操作

以一个名为q的优先队列为例:

q.size();//返回q里元素个数

q.empty();//返回q是否为空,空则返回1,否则返回0

q.push(k);//在q的末尾插入k

q.pop();//删掉q的第一个元素

q.top();//返回q的第一个元素(即是返回优先队列中有最高优先级的元素)

优先队列的特性
上文已经说过了,自动排序
怎么个排法呢?在这里介绍一下:

默认的优先队列(非结构体结构)

priority_queue <int> q;

这样的优先队列是怎样的?让我们写程序验证一下。

#include< cstdio >
#include< queue >
using namespace std;
priority_queue <int> q;
int main()
{
	q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
	while(!q.empty())
		printf("%d ",q.top()),q.pop();
}

程序大意就是在这个优先队列里依次插入10、8、12、14、6,再输出。
结果是什么呢?

14 12 10 8 6

也就是说,它是按从大到小排序的!

默认的优先队列(结构体,重载小于)

先看看这个结构体是什么。

struct node
{
	int x,y;
	bool operator < (const node & a) const
	{
		return x<a.x;
	}
};

这个node结构体有两个成员,x和y,它的小于规则是x小者小。
再来看看验证程序:

#include< cstdio >
#include< queue >
using namespace std;
struct node
{
	int x,y;
	bool operator < (const node & a) const
	{
		return x<a.x;
	}
}k;
priority_queue <node> q;
int main()
{
	k.x=10,k.y=100; q.push(k);
	k.x=12,k.y=60; q.push(k);
	k.x=14,k.y=40; q.push(k);
	k.x=6,k.y=80; q.push(k);
	k.x=8,k.y=20; q.push(k);
	while(!q.empty())
	{
		node m=q.top(); q.pop();
		printf("(%d,%d) ",m.x,m.y);
	}
}

程序大意就是插入(10,100),(12,60),(14,40),(6,20),(8,20)这五个node。
再来看看它的输出:

(14,40) (12,60) (10,100) (8,20) (6,80)

它也是按照重载后的小于规则,从大到小排序的。

**

less和greater优先队列:

**
还是以int为例,先来声明:

priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;

再次强调:“>”不要两个拼在一起。

话不多说,上程序和结果:

#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
int a[5]={10,12,14,6,8};
int main()
{
	for(int i=0;i<5;i++)
		p.push(a[i]),q.push(a[i]);
		
	printf("less<int>:")while(!p.empty())
		printf("%d ",p.top()),p.pop();	
		
	printf("\ngreater<int>:")while(!q.empty())
		printf("%d ",q.top()),q.pop();
}

结果:

less<int>:14 12 10 8 6
greater<int>:6 8 10 12 14

所以,我们可以知道,less是从大到小,greater是从小到大

作个总结:

为了装13方便,在平时,建议大家写:

priority_queue<int,vector<int>,less<int> >q;
priority_queue<int,vector<int>,greater<int> >q;

平时如果用从大到小不用后面的vector< int >,less< int >,可能到时候要改成从小到大,你反而会搞忘怎么写greater< int >,反而得不偿失。

另一种排序方法

有可能遇到这种情况:不想用重载小于一个结构体的优先队列,要按照各种不一样的规则排序。

当然,如果不是优先队列而是数组,我们就会多写几个bool函数塞到sort里面来改变它的小于规则,比如:

struct node
{
	int fir,sec;
}arr[2030];

bool cmp1(node x,node y)
{
	return x.fir<y.fir;  //当一个node x的fir值小于另一个node y的fir值时,称x<y
}

bool cmp2(node x,node y)
{
	return x.sec<y.sec;  //当一个node x的sec值小于另一个node y的sec值时,称x<y
}

bool cmp3(node x,node y)
{
	return x.fir+x.sec<y.fir+y.sec;  //当一个node x的fri值和sec值的和小于另一个node y的fir值和sec值的和时,称x<y
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d %d",&arr[i].fir,&arr[i].sec);
	
	puts("\n--------------------");
	sort(arr+1,arr+1+n,cmp1); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec);
}

	puts("\n--------------------");
	sort(arr+1,arr+1+n,cmp2); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec);
}

	puts("\n--------------------");
	sort(arr+1,arr+1+n,cmp3); for(int i=1;i<=n;i++) printf("%d. {%d %d}\n",i,arr[i].fir,arr[i].sec);
}

因为不是整体所以就省略了验证环节(也就是说上面那个代码的正确性不保证)

但是优先队列可没有sort那么灵活想用什么作小于规则用什么作小于规则,它只会用一个固定的小于规则。
所以如果想把一个队列按不同的方式优先,就要:

#include< queue >
#include< cstdio >
#include< cstring >
#include< iostream >
#include< algorithm >
using namespace std;

int n;
struct node
{
	int fir,sec;
	void Read() {scanf("%d %d",&fir,&sec);}
}input;

struct cmp1
{
	bool operator () (const node &x,const node &y) const
	{
		return x.fir<y.fir;
	}
};//当一个node x的fir值小于另一个node y的fir值时,称x<y

struct cmp2
{
	bool operator () (const node &x,const node &y) const
	{
		return x.sec<y.sec;  
	}
};//当一个node x的sec值小于另一个node y的sec值时,称x<y

struct cmp3
{
	bool operator () (const node &x,const node &y) const
	{
		return x.fir+x.sec<y.fir+y.sec; 
	}
};//当一个node x的fri值和sec值的和小于另一个node y的fir值和sec值的和时,称x<y

priority_queue<node,vector<node>,cmp1> q1;
priority_queue<node,vector<node>,cmp2> q2;
priority_queue<node,vector<node>,cmp3> q3;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) input.Read(),q1.push(input),q2.push(input),q3.push(input);
	
	printf("\ncmp1:\n");
	while(!q1.empty()) printf("(%d,%d) ",q1.top().fir,q1.top().sec),q1.pop();	
		
	printf("\n\ncmp2:\n");
	while(!q2.empty()) printf("(%d,%d) ",q2.top().fir,q2.top().sec),q2.pop();	
		
	printf("\n\ncmp3:\n");
	while(!q3.empty()) printf("(%d,%d) ",q3.top().fir,q3.top().sec),q3.pop();	
}

读入:

7
1 2
2 1
6 9
9 6
-100 100
-500 20
4000 -3000

输出:

cmp1:
(4000,-3000) (9,6) (6,9) (2,1) (1,2) (-100,100) (-500,20)

cmp2:
(-100,100) (-500,20) (6,9) (9,6) (1,2) (2,1) (4000,-3000)

cmp3:
(4000,-3000) (6,9) (9,6) (1,2) (2,1) (-100,100) (-500,20)

我们可以发现啊,priority_queue <int,vector< int >,less< int > > p;的那个less其实就代表这个优先队列的小于规则,所以把这个换成cmp1就会有上述效果,desu~
所以说,所以说啦,一定要记得写全称!
搞定!

总结:

优先队列到此就作了个小结。
其实不管是队列,还是优先队列,都不仅仅只有我讲的这些,还有更多可以探索。

应该入门级别都讲得差不多了,之后呢,在下就期待诸君的表演了。
学,无止境。

UPD (2018.11.03)
估摸着本文就讲了几个用法。

一句话,优先队列,是一种可以自动排序的队列。

首先是优先队列的各种声明方法。

#include<queue>
using namespace std;     

要加上这两个头文件。

最完整的声明公式(吧)形如:

priority_queue< 结构名, vector<结构名> , greater/less<结构名> > 队列名;

可以简写为priority_queue<结构名>,不过这样只能从大到小了。

三个结构名请保持一致,如int,double,long long,包括结构体(struct等)。
要求是这个结构要有小于的规则——你要告诉它怎么比较大小,它才能帮你排序。
系统自带的数据结构的小于规则是显然的,对于结构体,需要通过重载运算符等方式规定,如:

struct point
{
	int x,y;
	bool operator < (const point &p) const
	{
		return x*x+y*y<p.x*p.x+p.y*p.y;
		//假设这是平面上的两个点,规定两个点的大小关系为距离原点的距离小者小。
		//这个函数的意思是,当你使用小于运算符判断一个点(假设是a)与另一个点
		//(函数里的p)的大小关系时,系统会判断a.x*a.x+a.y*a.y是否<p.x*p.x+p.y*p.y
		//如果上述式子成立,就说明a点是小于p点的 ( return 1 ; ->小于运算符得出的结果为真)
	}
};
priority_queue<point> QP;

greater代表升序,即从小到大,
less代表降序,即从大到小,与缩减版无异。
如果不想用重载小于,就新建一个结构体并重载等号,在第三项里填入这个结构体的名字。

题目:合并果子

NOIP提高组2004 合并果子题目及其题解

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[10003]={},sum[10003]={},t,r;
    priority_queue<int ,vector<int>,greater<int> >Q;
    long long sum2=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    for(int j=0;j<n;j++){
        Q.push(a[j]);
    }
    for(int k=0;k<n-1;k++){
        t=Q.top();
        Q.pop();
        r=Q.top();
        Q.pop();
        Q.push(t+r);
        sum2+=t+r;
    }
    printf("%lld\n",sum2);
    return 0;
}

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值