http://www.cplusplus.com/reference/queue/priority_queue/

priority_queue 的top始终保持着为一堆数据中的最大元素。

读取最小 O(1)

插入和删除 lg(n)

(真是又简便又好用,难怪g不要我,当时连这个都不会写,sigh...)

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. class greater_class{
  7. public:
  8. bool operator()(int a,int b)
  9. {
  10. return a > b;
  11. }
  12. };
  13. int main()
  14. {
  15. vector<int> num;
  16.  
  17. priority_queue<int,vector<int>,greater_class> myQueue;
  18.  
  19. myQueue.push();
  20. myQueue.push();
  21. myQueue.push();
  22.  
  23. cout<< myQueue.top();
  24. myQueue.pop();
  25. myQueue.push();
  26.  
  27. cout<<"print all data in priority_queue"<<endl;
  28. while(myQueue.empty() == false)
  29. {
  30. cout<< myQueue.top() << " ";
  31. myQueue.pop();
  32. }
  33. }

priority_queue 示例的更多相关文章

  1. STL中的优先级队列priority_queue

    priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...

  2. 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例

    一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...

  3. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  4. (转)【C++ STL】细数C++ STL 的那些事 -- priority_queue(优先队列)

    装载自http://blog.csdn.net/tianshuai1111/article/details/7652553 一,概述 priority_queue是拥有权值观念的queue,它允许加入 ...

  5. 【转】priority_queue优先队列

    转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...

  6. 牛客网 牛客练习赛7 B.购物-STL(priority_queue)

    B.购物 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 在遥远的东方,有一家糖果专卖店 这家糖果 ...

  7. priority_queue用法(转载)

    关于priority_queue 1,关于STL中的priority_queue:确定用top()查看顶部元素时,该元素是具有最高优先级的一个元素. 调用pop()删除之后,将促使下一个元素进入该位置 ...

  8. STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较

    栈stack  .队列queue  和优先级priority_queue 三者比较 默认下stack 和queue 基于deque 容器实现,priority_queue 则基于vector 容器实现 ...

  9. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

随机推荐

  1. Excel 读写程序 C#源代码下载

    http://u.163.com/lNaJAjOz  提取码: E4ZHjnfD

  2. VB.net 调用dll

    Public Declare Function APlayer_OpenA Lib "APlayerCaller.dll" (ByVal aplayer As Integer, B ...

  3. git代理设置方法解决

    git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0. ...

  4. lua面试基础知识

    1.lua中八种基础类型:nil(空),boolean(布尔),number(数字),string(字符串),userdata(自定义类型),function(函数),thread(线程),table ...

  5. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans

    Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...

  6. [原创][LaTex]汇总博文

    0. 简介 为了查找方便,当当当当,灵机一动的设计了这个博文,有了他就能快速的查找一些该话题相关方面的一些博文了,不管是原创.转载.整理,只要是属于我自己整理的LaTex知识,应该都可以在这里找到的. ...

  7. 解决程序出现“terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)”的问题

    最近跑程序时出现了这么一个问题: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_al ...

  8. C#中线程对控件的访问

    Control类提供了一个Invoke方法来给子线程访问主线程的控件,它的原型是酱紫的: object.Control.Invoke(Delegate method); object.Control. ...

  9. web页面全角&半角

    根据Unicode编码,全角空格为12288,半角空格为32 : 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248  全角-->半角函数  //半角转换 ...

  10. 启动Tomcat时报 Expected stackmap frame at this location.(JDK1.7编译)

    从svn上下的项目,部署到tomcat 7.0.19 上, 并且配置的是jdk7.  启动时出现以下问题. Location: com/genlot/loms/service/SysPermissio ...