Welcomechhaya.blog.chinaunix.net
chhaya
全部博文(117)
总结(3)
模拟 水题(6)
计算几何(1)
数论(3)
不好分类(8)
查找(5)
并查集(2)
图论(24)
搜索(11)
贪心(2)
DP(4)
2012年(5)
2011年(5)
2010年(46)
2009年(61)
untouche
athsonxy
five_eas
cynthia
xuleilei
老顽童熊
kelly_sm
芸锺鹤
今天不吃
jasonwan
chxk123
分类: C/C++
2010-08-01 09:53:52
/* 基本数据类型 */ #include <iostream> #include <queue> using namespace std; int main() { int i; int a[10]; priority_queue<int, vector<int>, greater<int> >big; priority_queue<int, vector<int>, less<int> > small; for(i=0; i<10; i++) { a[i] = rand() % 10; big.push(a[i]); small.push(a[i]); } for(i=0; i<10; i++) { cout << a[i] << ' '; } cout << endl; while(!big.empty()) { cout << big.top() << " "; big.pop(); } cout << endl; while(!small.empty()) { cout << small.top() << " "; small.pop(); } return 0; }
/× 自定义结构 ×/
#include <iostream> #include <queue> using namespace std; struct stata { int a, b; stata(int aa=0, int bb=0):a(aa),b(bb) {} }; bool operator<(const stata x, const stata y) { return x.a < y.a; } struct cmp { bool operator()(stata x, stata y) { return x.a > y.a; } }; int main() { priority_queue <stata> big; //最大优先 priority_queue <stata, vector<stata>, cmp> small; //最小优先 int aa[10], bb[10]; for(int i =0; i < 10; i++) { aa[i] = rand() % 20; bb[i] = rand() % 20; big.push(stata(aa[i], bb[i])); small.push(stata(aa[i], bb[i])); } cout << "from big to small:" << endl; while(!big.empty()) { cout << big.top().a << ' ' << big.top().b << endl; big.pop(); } cout << endl; cout << "from small to big:" << endl; while(!small.empty()) { cout << small.top().a << ' ' << small.top().b << endl; small.pop(); } cout << endl; return 0; }
上一篇:poj 3322 Bloxorz I [bfs]
下一篇:POJ 3221 Diamond Puzzle[bfs]
登录 注册