STL之priority_queue
2015-08-07 11:34
363 查看
#include <iostream> #include<functional> #include <queue> using namespace std; struct Node{ int x; Node( int a= 0):x(a) {} friend bool operator<(Node a,Node b ) { return a.x<b.x;// 使用">"则为小根堆 } }; struct cmp { bool operator()(Node a,Node b) { return a.x<b.x; } }; int main(){ int data2[5]={1,3,7,-4,56}; /******方法一*/ priority_queue<int> big(data2,data2+5); while(!big.empty()) { cout<<big.top()<<' '; big.pop(); } cout<<endl; /******方法二*/ priority_queue<int,vector<int>,cmp> little(data2,data2+5); //priority_queue<int,vector<int>,greater<int>> little(data2,data2+5); while(!little.empty()) { cout<<little.top()<<' '; little.pop(); } cout<<endl; /******方法三*/ Node data1[10]; for( int i= 0; i< 10; ++i ) data1[i]=Node(rand()); priority_queue<Node> q(data1,data1+10);//自定义大根模式 while( !q.empty() ) { cout << q.top().x << ' ' ; q.pop(); } cout<<endl; return 0; }
相关文章推荐
- SWT中的UI线程
- Vagrant is attempting to interface with the UI in a way that requires a TTY
- 再说bluetooth4.2
- Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi
- StringBuffer与StringBuilder 区别
- Jsp的request
- UVALive 5881 Unique Encryption Keys
- js生成UUID
- setValue: forKeyPath:
- 浅谈:UEO就是SEO的一种
- Android 重写系统控件UI,自定义进度条
- 超详细!iOS 并发编程之 Operation Queues
- druid连接无法回收...
- 解决 UITextView 没有 ShouldReturn 事件
- iOS之整型转换警告Values of type 'NSInteger' should not be used as format arguments;
- Language Guide (proto3)
- UIScrollView 原理详解
- Fruit
- editui刷新数据与列表循环
- easyui阻止事件冒泡