结构体的优先队列
2015-09-08 11:24
435 查看
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 123123123
#define MAX_INDEX 100005
using namespace std;
struct node
{
double a;
string name;
bool operator > (const node &other) const
{
if ((a > other.a))
{
return true;
}
return false;
}
bool operator < (const node &other) const
{
if ((a < other.a))
{
return true;
}
return false;
}
};
int main()
{
priority_queue<node,vector<node>,greater<node> >q;
//注意:这里使用了greater的话,那node结构体里必须重载了>比较符!
//如果没有使用greater,那就是小根堆,那node里必须重载<比较符!
node temp;
temp.name = "s3";
temp.a = 92.78;
q.push(temp);
temp.name = "s2";
temp.a = 91.66;
q.push(temp);
temp.name = "s1";
temp.a = 91.78;
q.push(temp);
temp.name = "s4";
temp.a = 91.41;
q.push(temp);
while(!q.empty())
{
node temp1 = q.top();
q.pop();
cout << temp1.name << " = " << temp1.a << endl;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 123123123
#define MAX_INDEX 100005
using namespace std;
struct node
{
double a;
string name;
bool operator > (const node &other) const
{
if ((a > other.a))
{
return true;
}
return false;
}
bool operator < (const node &other) const
{
if ((a < other.a))
{
return true;
}
return false;
}
};
int main()
{
priority_queue<node,vector<node>,greater<node> >q;
//注意:这里使用了greater的话,那node结构体里必须重载了>比较符!
//如果没有使用greater,那就是小根堆,那node里必须重载<比较符!
node temp;
temp.name = "s3";
temp.a = 92.78;
q.push(temp);
temp.name = "s2";
temp.a = 91.66;
q.push(temp);
temp.name = "s1";
temp.a = 91.78;
q.push(temp);
temp.name = "s4";
temp.a = 91.41;
q.push(temp);
while(!q.empty())
{
node temp1 = q.top();
q.pop();
cout << temp1.name << " = " << temp1.a << endl;
}
return 0;
}
相关文章推荐
- CentOS下添加新硬盘
- Java框架介绍:Quartz从入门到进阶(图)
- 移除百度地图LOGO和版权信息
- C++模板
- C#高级------移动存储设备案例
- [LeetCode]题解(python):005-Longest Palindromic Substring
- [转]SSAS没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) (Microsoft Visual Studio)的解决办法
- read 读取文件内容
- MySQL 不能以关键字做表名
- 堆栈、堆、静态存储区
- 登录信息提示
- 前端有关学习网址
- 【第2周 项目1 - C++语言中函数参数传递的三种方式】
- vs2010 如何让创建和使用动态链接库(dll)
- 字符串生成32位MD5码
- jquery easyui datagrid 对数据格式化(日期格式)
- <Swift>IOS开发Swift语言中应用CoreData之数据存取
- java实例化对象的方法
- sys密码格式导致OEM备份及数据泵报错
- artTemplate使用说明