priority_queue详解
priority_queue是一个安排好的顺序存储的队列,队首是优先级最高的元素。
Template<class T , class Container = vector<T> , class compare = less<T>>
第一个参数是priority_queue保存的元素类型T,Container是配置的底层容器,priority_queue默认使用了底层容器vector,但也可以使用deque,但是绝对不能使用list,因为list是不能随机访问元素的。Less是一个类模板,支持两个类型为T的元素进行operator<运算,来进行比较。(比较后得到优先级)
优先级越大离队首越近;,通过top()方法可以返回队首元素的const&,
#include <iostream>
#include <queue>
class Error
{
public:
Error(int priority,std::string errorString)
:mPriority(priority),mErrorString(errorString)
{} int getPriority(){return mPriority;}
std::string getErrorString(){return mErrorString;}
friend bool operator <(const Error& lhs , const Error &rhs);
friend std::ostream &operator << (std::ostream &os, Error& rhs);
private:
std::string mErrorString;
int mPriority;
};
bool operator <(const Error& lhs , const Error &rhs)
{
return lhs.mPriority < rhs.mPriority;
} std::ostream &operator << (std::ostream &os,Error& rhs)
{
os << rhs.getErrorString() << "priority : " << rhs.getPriority() << std::endl;
return os;
} class ErrorCorrelateor
{
public:
void addError(const Error & error);
Error getError(); private:
std::priority_queue<Error> mError;
}; void ErrorCorrelateor::addError(const Error & error)
{
mError.push(error);
} Error ErrorCorrelateor::getError()
{
if(mError.empty())
{
throw std::out_of_range("priority queue is empty\r\n");
} else
{
Error tempError = mError.top();
mError.pop();
return tempError;
}
} int main() {
ErrorCorrelateor correlateor;
correlateor.addError(Error(,"Unable to read file"));
correlateor.addError(Error(,"Incorrect entry User"));
correlateor.addError(Error(,"Unable collacate memery"));
correlateor.addError(Error(,"Unable write file")); while(true)
{
try
{
Error ec = correlateor.getError();
std::cout << ec ;
}catch (const std::out_of_range&)
{
std::cout << "Finised Error correlatetor" << std::endl;
break;
}
}
return ;
}
结果是:
Unable collacate memerypriority : 9
Incorrect entry Userpriority : 5
Unable write filepriority : 5
Unable to read filepriority : 1
Finised Error correlatetor
priority_queue详解的更多相关文章
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- multimap 和priority_queue详解
上一期是关于STL和并查集结合的例题,也附了STL中部分容器的使用摘要,由于是从网上东拼西凑的,感觉有的关键点还是没解释清楚,现在从其中摘出两个容器,用例题对它们的用法进行进一步解释. 以下是例题的介 ...
- C++ STL 优先队列 priority_queue 详解(转)
转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...
- 优先队列priority_queue详解
转载链接
- 详解C++ STL priority_queue 容器
详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
- C++ STL详解
C++ STL详解 转载自:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 一.STL简介 STL(Standard ...
- 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)
如约而至,Java 10 正式发布! 3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...
随机推荐
- 单源最短路(Dijkstra算法)
#返回上一级 @Author: 张海拔 @Update: 2015-03-11 @Link: http://www.cnblogs.com/zhanghaiba/p/3514570.html Dijk ...
- JS上传图片转化成Base64编码demo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- <Android 基础(二十四)> EditText
介绍 A text field allows the user to type text into your app. It can be either single line or multi-li ...
- 微服务&spring cloud架构系列汇总
为了方便查找,把微服务&微服务架构之spring cloud架构系列文章按时间正序整理了一下,记录如下: 1. 微服务架构之spring cloud 介绍 2. 微服务架构之spring ...
- MPU/SoC/Application Processor/Embedded OS
Everything has its principles and mechanisms which are designed by its creator and followed by its u ...
- 【转】pscp实现远程文件(夹)传输
原文地址:http://blog.163.com/yang_jianli/blog/static/16199000620128251383197/ pscp与linux下的scp命令相似,功能相同,在 ...
- token 和 服务器端auth0-JWT包的使用
移动客户端和服务器端在用户登录上经常使用的是一个叫做token的认证方法 token是什么? token顾名思义, 令牌, 意思就是说,第一次用户登录验证之后,服务器返回一个token令牌,之后客户端 ...
- Mac系统查看端口占用和杀死进程
查看进程占用 lsof -i tcp:8080 该命令会显示占用8080端口的进程,有其 pid ,可以通过pid关掉该进程 杀死进程 kill pid 例如 kill 39394 转自:https: ...
- visual box 安装 centos7后,无法上网
ifconfig 命令后,只看到个回环网卡: 进入 /etc/sysconfig/network-scripts后,发现有设备 visual box 中设置为“桥接网卡”,也没问题,最后把控制芯片改 ...
- August 16th 2017 Week 33rd Wednesday
A man can be destroyed but not defeated. 一个人可以被毁灭,但不能被打败. Before he was destroyed, he would have bee ...