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详解的更多相关文章

  1. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  2. multimap 和priority_queue详解

    上一期是关于STL和并查集结合的例题,也附了STL中部分容器的使用摘要,由于是从网上东拼西凑的,感觉有的关键点还是没解释清楚,现在从其中摘出两个容器,用例题对它们的用法进行进一步解释. 以下是例题的介 ...

  3. C++ STL 优先队列 priority_queue 详解(转)

    转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...

  4. 优先队列priority_queue详解

    转载链接

  5. 详解C++ STL priority_queue 容器

    详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...

  6. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  7. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  8. C++ STL详解

    C++ STL详解 转载自:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 一.STL简介 STL(Standard ...

  9. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

随机推荐

  1. H5演示文稿快速制作

    详见: http://www.geekfan.net/8107/ 或 http://www.jianshu.com/p/09a3bbb8b362

  2. mongdb启动报错

    2018-08-19T12:25:31.707+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1 ...

  3. Springboot @ResponseBody返回中文乱码

    最近我在把Spring 项目改造Springboot,遇到一个问题@ResponseBody返回中文乱码,因为response返回的content-type一直是application/json;ch ...

  4. java.lang.StackOverflowError: null

    异常信息 java.lang.StackOverflowError: null at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.dtoa(Floatin ...

  5. 【PyQt5 学习记录】004:简单QThread笔记

    在文本编辑框中每隔几秒添加一行文本,代码如下: #!/usr/bin/python3 # -*- coding:utf-8 -*- import sys from PyQt5.QtWidgets im ...

  6. 一行代码解决各种IE的兼容问题

    一行代码解决各种IE的兼容问题  在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 < ...

  7. C# 读取excel用户列表过滤一个月内未收到外部邮件已离职的员工

    1.通过aspose.cells读取excel中的数据并添加到list中 //存储从excel中读取出来的数据 List<UserInfo> lst_userinfo = new List ...

  8. 统计nginx日志

    .根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l .统计访问URL统计PV awk '{print $7}' access.l ...

  9. Selenium+java项目测试问题整理

    一.页面跳转到另一链接 问题描述:打开页面链接为A.com,但是页面元素需跳转到链接B.com.这时B页面将无法识别该元素,导致拨错 解决方案:重新自定义驱动,打开新链接 (PS:比较笨的解决方法,但 ...

  10. Ionic控件之——按钮(Button)

    Ionic提供丰富的按钮特性,足以满足大部分的按钮实现需求. 一.HTML实现一个简单按钮: <button class="button"> 我是按钮 </but ...