STL 优先队列(队列+栈) 转载

来源:互联网 发布:数据泄漏防护系统 破解 编辑:程序博客网 时间:2024/04/29 12:58

转载出处:http://blog.sina.com.cn/s/blog_5e518b010100kbts.html

转载出处:http://blog.csdn.net/baochunzhi/article/details/7664422

STL之优先队列

原本以为priority_queue很简单,才知道原来懂的只是最简单的形式。

头文件:#include<queue>

优先队列,也就是原来我们学过的堆,按照自己定义的优先级出队时。默认情况下底层是以Vector实现的heap

既然是队列,也就只有入队、出队、判空、大小的操作,并不具备查找功能。

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

用途就不用多说了吧,例如Huffman编码、分支限界、A*启发式都需要用到优先队列存放信息。

来看最常用的几个功能,了解一下其中的知识点:

一:最基本的功能

#include<iostream>

#include<queue>

using namespace std;

int main()

{

    priority_queue<int> Q;

    Q.push(2);

    Q.push(5);

    Q.push(3);

    while(!Q.empty())

    {

           cout<<Q.top()<<endl;

           Q.pop();

    

    system("pause");

    return 0;

}

  

优先队列最基本的功能就是出队时不是按照先进先出的规则,而是按照队列中优先级顺序出队。

知识点:1、一般存放实型类型,可比较大小

2、默认情况下底层以Vector实现

3、默认情况下是大顶堆,也就是大者优先级高,后面可以自定义优先级比较规则

二:次基本的功能

#include<iostream>

#include<queue>

using namespace std;

int main()

{

    int a[5]={3,4,5,2,1};

    priority_queue<int> Q(a,a+5);

    while(!Q.empty())

    {

           cout<<Q.top()<<endl;

           Q.pop();

    

    system("pause");

    return 0;

}

可以将一个存放实型类型的数据结构转化为优先队列,这里跟优先队列的构造函数相关。

上面那个默认构造一个空的优先队列,什么都使用默认的。

而这里使用的是

Priority_queue(InputIterator first,InputIterator last)

我理解的就是给出了一个容器的开口和结尾,然后把这个容器内容拷贝到底层实现(默认vector)中去构造出优先队列。这里也使用了一个默认的比较函数,也是默认大顶堆

三 应该掌握的功能:

 

#include<iostream>

#include<queue>

using namespace std;

 

typedef pair<long,int> Node;

 

priority_queue< Node,vector< Node >,greater< Node > > Q;

 

 

这个里面定义了一个制定存放元素(Node),底层实现以vector实现(第二个参数),优先级为小顶堆(第三个参数)

前两个参数没什么说的,很好理解,其中第三个参数,默认有三写法:

小顶堆:greater<TYPE>

大顶堆:less<TYPE>

如果想自定义优先级而TYPE不是基本类型,而是复杂类型,例如结构体、类对象,则必须重载其中的operator(),见下面的例子。

 

 

 

例子:

#include<iostream>

#include<queue>

using namespace std;

 

//模拟存放节点信息

typedef struct

{

int a;

int b;      

}Node;

//自定义优先级类型

struct cmp

{

       bool operator()(const Node &t1,const Node &t2)

       {

            return t1.b<t2.b;//相当于less,大顶堆    

       }

};

int main()

{

    //初始化

   int n;

   cin>>n;

   Node *arr=new Node[n];

   for(int i=0;i<n;i++)

   {

          cin>>arr[i].a>>arr[i].b;       

   }

   //定义优先队列 ,自定义优先级,Qsort里面自定义相似

   priority_queue<Node,vector<Node>,cmp> Q(arr,arr+n);

   while(!Q.empty())

   {

         Node n=Q.top();

         cout<<n.a<<" "<<n.b<<endl;

         Q.pop();             

   }

    system("pause");

    return 0;

}

 

 

STL 中优先队列的使用方法(priority_queu)

基本操作:

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

pop() 删除对顶元素

push() 加入一个元素

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

top() 返回优先队列对顶元素

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

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

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


 

2、自定义优先级:

struct cmp
{
operatorbool ()(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
booloperator< (node a, node b)
{
return a.x > b.x; //结构体中,x小的优先级高
}
};
priority_queue
<node>q;//定义方法
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误



 

STL 中队列的使用(queue)

基本操作:

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

使用方法:

头文件:

#include <queue>


声明方法:

1、普通声明

queue<int>q;


2、结构体

struct node
{
int x, y;
};
queue
<node>q;



 

STL 中栈的使用方法(stack)

基本操作:

push(x) 将x加入栈中,即入栈操作

pop() 出栈操作(删除栈顶),只是出栈,没有返回值

top() 返回第一个元素(栈顶元素)

size() 返回栈中的元素个数

empty() 当栈为空时,返回 true


使用方法:

和队列差不多,其中头文件为:

#include <stack>


定义方法为:

stack<int>s1;//入栈元素为  int 型
stack<string>s2;//  入队元素为string型
stack<node>s3;//入队元素为自定义型



 


/**//*
*===================================*
| |
| STL中优先队列使用方法 |
| |
| chenlie |
| |
| 2010-3-24 |
| |
*===================================*
*/
#include
<iostream>
#include
<vector>
#include
<queue>
usingnamespace std;
int c[100];

struct cmp1
{
booloperator ()(int x,int y)
{
return x > y;//小的优先级高
}
};

struct cmp2
{
booloperator ()(constint x,constint y)
{
return c[x] > c[y];
// c[x]小的优先级高,由于可以在对外改变队内的值,
//所以使用此方法达不到真正的优先。建议用结构体类型。
}
};

struct node
{
int x, y;
friend
booloperator< (node a, node b)
{
return a.x > b.x;//结构体中,x小的优先级高
}
};


priority_queue
<int>q1;

priority_queue
<int, vector<int>, cmp1>q2;

priority_queue
<int, vector<int>, cmp2>q3;

priority_queue
<node>q4;


queue
<int>qq1;
queue
<node>qq2;

int main()
{
int i, j, k, m, n;
int x, y;
node a;
while (cin >> n)
{
for (i =0; i< n; i++)
{
cin
>> a.y >> a.x;
q4.push(a);
}
cout
<< endl;
while (!q4.empty())
{
cout
<< q4.top().y<<""<< q4.top().x<< endl;
q4.pop();
}
// cout << endl;
}
return0;
}

 

 

 

 

 

0 0
原创粉丝点击