栈与优先队列的使用_STL
2014-06-08 20:53
232 查看
STL 中栈的使用方法(stack)
#include <stack>
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
STL 中队列的使用(queue)
#include <queue>
基本操作:
push(x) 将x压入队列的末端
pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值
front() 返回第一个元素(队顶元素)
back() 返回最后被压入的元素(队尾元素)
empty() 当队列为空时,返回true
size() 返回队列的长度
STL 中优先队列的使用详细介绍(priority_queu)
#include <queue>
基本操作:
empty() 如果队列为空返回真
pop() 删除对列首元素
push() 加入一个元素
size() 返回优先队列中拥有的元素个数
top() 返回优先队列首元素
在默认的优先队列中,优先级高的先出队。
标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系。
(1)优先队列的第一种用法,这是最常用的默认的优先级用法,也就是优先级高的先出队列,例如说一个int优先队列,那么出队的时候就是int大的先出队列。
下面给出一个例子:
运行结果可想而知,就是从大到小的: 5 4 3 2 1
(2)那么如果想要优先队列中低优先级的元素先出队列,怎么办呢? --- 自定义优先级
①方法一:我们可以传入一个比较函数,使用functional头文件中的greater函数对象作为比较函数
注意:首先要添加头文件:#include <functional>
priority_queue< int,vector<int>,greater<int> > q; // 注意:> > 误写成>> 会报错
这样我们就创建了一个低优先级元素先出对列的优先队列。
修改上面的例子,运行,就会发现,输出的顺序变成了:1 2 3 4 5。
当然,与greater相对的less,如果传入less这个比较函数,那么就是高优先级元素先出队列了。
priority_queue< int,vector<int>,less<int> > q;
priority_queue<int> q;
以上创建的优先队列是相同的。
②方法二:自己实现比较函数
priority_queue<int,vector<int>,cmp1 > q;
这样就创建了一个小值元素先出队列的优先队列,这里的 cmp1 作用就相当于 greater
同理我们可以写出:
priority_queue<int,vector<int>,cmp2 > q;
这里就是创建了一个大值元素先出队列的优先队列,这里的 cmp2 作用也就是相当于 less
(3)假如优先队列中的元素是一个结构对象或者是类对象,那么如何重新自定义其优先级比较呢?
例子一:定义一个结构体,这个结构体只有一个元素 x 。
①低优先级元素先出队列,也就是x值小的先出队列。
输出: 1 2 3 4 5
②高优先级元素先出队列,也就是x值大的先出队列。
注意到:结构体中重载的运算符只可以是 <, 因为标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系,如果重载 > ,那么会编译错误。
例子二:在上面的例子中,我们是将 结构体中的 x 的大小当做是优先级比较值了。下面给出另一例子,这个例子更具有一般性。
在这个结构体中,priority表征优先级值。
输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
#include <stack>
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
STL 中队列的使用(queue)
#include <queue>
基本操作:
push(x) 将x压入队列的末端
pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值
front() 返回第一个元素(队顶元素)
back() 返回最后被压入的元素(队尾元素)
empty() 当队列为空时,返回true
size() 返回队列的长度
STL 中优先队列的使用详细介绍(priority_queu)
#include <queue>
基本操作:
empty() 如果队列为空返回真
pop() 删除对列首元素
push() 加入一个元素
size() 返回优先队列中拥有的元素个数
top() 返回优先队列首元素
在默认的优先队列中,优先级高的先出队。
标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系。
(1)优先队列的第一种用法,这是最常用的默认的优先级用法,也就是优先级高的先出队列,例如说一个int优先队列,那么出队的时候就是int大的先出队列。
下面给出一个例子:
#include <iostream> #include <queue> using namespace std; int main() { priority_queue<int> q; for(int i = 1;i <= 5;i++) { q.push(i); } for(int i = 0;i < 5;i++) { cout<<q.top()<<endl; q.pop(); } return 0; }
运行结果可想而知,就是从大到小的: 5 4 3 2 1
(2)那么如果想要优先队列中低优先级的元素先出队列,怎么办呢? --- 自定义优先级
①方法一:我们可以传入一个比较函数,使用functional头文件中的greater函数对象作为比较函数
注意:首先要添加头文件:#include <functional>
priority_queue< int,vector<int>,greater<int> > q; // 注意:> > 误写成>> 会报错
这样我们就创建了一个低优先级元素先出对列的优先队列。
修改上面的例子,运行,就会发现,输出的顺序变成了:1 2 3 4 5。
当然,与greater相对的less,如果传入less这个比较函数,那么就是高优先级元素先出队列了。
priority_queue< int,vector<int>,less<int> > q;
priority_queue<int> q;
以上创建的优先队列是相同的。
②方法二:自己实现比较函数
struct cmp1 { bool operator ()(int x,int y) { return x>y; //小值优先 } };
priority_queue<int,vector<int>,cmp1 > q;
这样就创建了一个小值元素先出队列的优先队列,这里的 cmp1 作用就相当于 greater
同理我们可以写出:
struct cmp2 { bool operator ()(int x,int y) { return x<y; //大值优先 } };
priority_queue<int,vector<int>,cmp2 > q;
这里就是创建了一个大值元素先出队列的优先队列,这里的 cmp2 作用也就是相当于 less
(3)假如优先队列中的元素是一个结构对象或者是类对象,那么如何重新自定义其优先级比较呢?
例子一:定义一个结构体,这个结构体只有一个元素 x 。
①低优先级元素先出队列,也就是x值小的先出队列。
struct number1 { int x; bool operator < (const number1 &a) const { return x>a.x;//小值优先 } };
number1 num1[5]; priority_queue<number1>q; for(int i = 1; i <= 5; i++) { num1[i].x = i; q.push(num1[i]); } for(int i = 1; i <= 5; i++) { cout<<q.top().x<<endl; q.pop(); }
输出: 1 2 3 4 5
②高优先级元素先出队列,也就是x值大的先出队列。
struct number2 { int x; bool operator < (const number2 &a) const { return x<a.x;//大值优先 } };
注意到:结构体中重载的运算符只可以是 <, 因为标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系,如果重载 > ,那么会编译错误。
例子二:在上面的例子中,我们是将 结构体中的 x 的大小当做是优先级比较值了。下面给出另一例子,这个例子更具有一般性。
struct node { friend bool operator < (node n1, node n2) { return n1.priority < n2.priority; } int priority; int value; };
在这个结构体中,priority表征优先级值。
priority_queue<node> qn; node b[5]; 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(int i = 0; i < 5; i++) qn.push(b[i]); cout<<"优先级"<<'\t'<<"值"<<endl; for(int i = 0; i < 5; i++) { cout<<qn.top().priority<<'\t'<<qn.top().value<<endl; qn.pop(); }
输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
相关文章推荐
- STL中优先队列的使用
- STL 优先队列 priority_queue 的使用
- STL中优先队列的使用
- STL优先队列,队列,栈的使用
- STL优先队列使用
- STL中优先队列的使用方法
- 【c++——STL】标准模板库STL中优先队列Priority Queues使用手册
- poj3253 Fence Repair 优先队列,C++STL中priority_queue的使用
- STL 中优先队列的使用方法(priority_queue)
- STL 中优先队列的使用方法(priority_queu)
- STL中优先队列的使用
- STL中优先队列的使用
- STL 中优先队列的使用方法(priority_queu)
- 标准模板库STL中优先队列Priority Queues使用手册
- STL 优先队列priority_queue 使用练习
- STL中优先队列的使用
- stl中优先队列的使用方法
- STL优先队列的使用
- UVA-LA 3135 2004年北京站C题 STL priority_queue自定义优先顺序优先队列的使用
- STL中的优先队列priority_queue的使用方法