STL优先级队列菜鸟有问题。
- C/C++ code
#include <iostream>#include <queue>#include <deque>#include <vector>#include <functional>using namespace std;//using priority_queue with deque//哪里体现了是用deque做的?//use of function greater sorts the items in ascending ordertypedef deque<int> INTDQU;typedef priority_queue<int> INTPRQUE;//using priority_queue with vector;//哪里体现了是用deque做的?//use of function less sorts the items in ascending ordertypedef vector<char> CHVECTOR;typedef priority_queue<char> CHPRQUE;void main(void){ int size_q; INTPRQUE q; CHPRQUE p; //Insert items in the priority_queue(uses deque) q.push(42); q.push(100); q.push(49); q.push(201); size_q=q.size(); cout<<"size of q is : "<<size_q<<endl; while(!q.empty()) { cout<<q.top()<<" "; q.pop(); } cout<<endl; //Insert items in the priority_queue(uses vector) p.push('c'); p.push('a'); p.push('d'); p.push('m'); p.push('h'); cout<<p.top()<<endl; while(!p.empty()) { cout<<p.top()<<" "; p.pop(); } cout<<endl;}
上面一段代码是从书上敲下来的?我就搞不懂了,哪里体现了q是用deque,p是用vector?
还有去掉下面这两行也没有问题啊。
typedef deque<int> INTDQU;
typedef vector<char> CHVECTOR;
最后一个问题,说用greater与less做升序和降序,也没看到影子呀。
⊙⊙b汗。。。。
[解决办法]
哪里体现了:
INTPRQUE q;//typedef priority_queue<int> INTPRQUE;
CHPRQUE p;//typedef priority_queue<char> CHPRQUE;
去掉下面这两行:没用上
typedef deque<int> INTDQU;
typedef vector<char> CHVECTOR;