优先队列&&队列

来源:互联网 发布:ssh连接阿里云 编辑:程序博客网 时间:2024/06/05 17:17

优先队列用法
转载:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:

基本操作:
empty() 如果队列为空返回真
pop() 删除对顶元素
push() 加入一个元素
size() 返回优先队列中拥有的元素个数
top() 返回优先队列对顶元素
在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。
使用方法:
头文件:
#include <queue>
声明方式:
1、普通方法:
priority_queue<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> qi;
通过<操作符可知在整数中元素大的优先级高。
故示例1中输出结果为:9 6 5 3 2

第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

priority_queue<int,vector<int>,greater<int>>qi2;
其中
第二个参数为容器类型。
第二个参数为比较函数。
故示例2中输出结果为:2 3 5 6 9

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<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误


第三种方法:
自定义优先级。

struct node
{
    friend booloperator< (node n1, node n2)
    {
       return n1.priority < n2.priority;
    }
    intpriority;
    intvalue;
};
在该结构中,value为值,priority为优先级。
通过自定义operator<操作符来比较元素中的优先级。
在示例3中输出结果为:
优先级 
        5
        2
        1
        3
        4
但如果结构定义如下:

struct node
{
    friend booloperator> (node n1, node n2)
    {
       return n1.priority > n2.priority;
    }
    intpriority;
    intvalue;
};
则会编译不过(G++编译器)
因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。

//代码清单

 #include<iostream>
#include<functional>
#include<queue>
using namespace std;
struct node
{
    friend booloperator< (node n1, node n2)
    {
       return n1.priority < n2.priority;
    }
    intpriority;
    intvalue;
};
int main()
{
    const intlen = 5;
    int i;
    int a[len] ={3,5,9,6,2};
    //示例1
   priority_queue<int> qi;
    for(i = 0; i< len; i++)
       qi.push(a[i]);
    for(i = 0; i< len; i++)
    {
       cout<<qi.top()<<"";
       qi.pop();
    }
   cout<<endl;
    //示例2
   priority_queue<int,vector<int>,greater<int>>qi2;
    for(i = 0; i< len; i++)
       qi2.push(a[i]);
    for(i = 0; i< len; i++)
    {
       cout<<qi2.top()<<"";
       qi2.pop();
    }
   cout<<endl;
    //示例3
   priority_queue<node> qn;
    nodeb[len];
   b[0].priority = 6; b[0].value = 1;
   b[1].priority = 9; b[1].value = 5;
   b[2].priority = 2; b[2].value = 3;
   b[3].priority = 8; b[3].value = 2;
   b[4].priority = 1; b[4].value = 4;

    for(i =0; i < len; i++)
       qn.push(b[i]);
   cout<<"优先级"<<'\t'<<"值"<<endl;
    for(i = 0; i< len; i++)
    {
       cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
       qn.pop();
    }
    return0;
}

 
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;//入队元素为自定义型


 


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int c[100];

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

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

struct node
{
int x, y;
friend bool operator < (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;
}
return 0;
}

0 0
原创粉丝点击