六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 64|回复: 0

priority_queue的用法!

[复制链接]

升级  93.8%

309

主题

309

主题

309

主题

进士

Rank: 4

积分
969
 楼主| 发表于 2013-1-15 01:56:42 | 显示全部楼层 |阅读模式
//学习于此博客:http://kmplayer.javaeye.com/blog/693965
#include<iostream>
#include<queue>
using namespace std;
/*/////////////////////////////////////////////////////////////
less<class T> 这是按值大的优先 greater<class T>这是按值小的优先
int main()
{
priority_queue<int>tt;//默认情况下,较大值有较高的优先权
for(int i=0;i<10;i++)
  tt.push (rand()%10);
while(!tt.empty ())
{
  cout<<tt.top ()<<" ";
  tt.pop ();
}
cout<<endl;
}
*////////////////////////////////////////////////////////

/*//////////////////////////////////////////////
int main()
{
priority_queue<int ,vector<int >,greater<int >  >tt;//加入了greater,较小值有较高的优先权
for(int i=0;i<10;i++) //这边的连续'> >'要与左移区分开!
  tt.push (rand()%10);
while(!tt.empty ())
{
  cout<<tt.top ()<<" ";
  tt.pop ();
}
cout<<endl;
}*/////////////////////////////////////////////////



/*///////////////////////////////////////////////
class node
{
public:
int first,second;//两个参数的优先队列
node(int a=0,int b=0):
first(a),second(b){}
};
bool operator<(node a,node b)//重载'<'符号
{
if(a.first ==b.first ) return a.second >b.second ;
return a.first >b.first ;
}
*/////////////////////////////////////////

class node
{
public:
int first,second;
node(int a,int b)
{
  first=a;second=b;
}
friend bool operator<(const node &x,const node &y)
{
  if(x.first> y.first )return true;
  if(x.first ==y.first )
   if (x.second>y.second)
    return true;
  return false;
}
friend ostream& operator<<(ostream &os,const node &td)
{
  return os<<td.first <<" "<<td.second <<endl;
}
};
int main()
{
priority_queue<node>tt;
tt.push (node(8,2));
tt.push(node(2,3));
while(!tt.empty ())
{
  cout<<tt.top ()<<endl;

  cout<<tt.top ().first <<" "<<tt.top ().second <<endl;
  tt.pop();
}
return 0;
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表