priority_queue为复合结构排序: 

 #include <iostream>
#include <queue> using namespace std;
struct Node{
int x;
string y;
Node( int a= , string b = "" ):
x(a), y(b) {}
};
bool operator<( Node a, Node b ){ // 注意这里的顺序和sort()里面的谓词函数不一样!
// bool为真的 优先级小
if( a.x == b.x ) return a.y < b.y;
return a.x < b.x;
}
//自定义重载小于操作符
int main(){
/********************************************
对于自定义类型,则必须自己重载 operator<
自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
看下面的例子
*******************************************/ cout<<"自定义: "<<endl;
priority_queue<Node> q2;
priority_queue<int> q3;
std::string tmp = ""; for( int i= ; i>; --i ){
tmp = tmp + "";
q2.push( Node(i, tmp) );
q3.push(i);
}
while( !q2.empty() ){
cout << q2.top().x << ' ' << q2.top().y << endl;
q2.pop();
}
while( !q3.empty() ){
cout << "q3 output: "<<q3.top() << endl;
q3.pop();
}
//注意上面不能这样来定义:priority_queue<Node, vector<Node>, greater<Node> >;
//这样定义是错误的!!!!
//原因是:greater<node>没有定义 //必须定义如下函数对象,才能这样定义:
// priority_queue<Node, vector<Node>, cmp >;
/*
struct cmp{
bool operator() ( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y; return a.x> b.x; }
};
*/
return ;
} root@u18:~/cp/test# g++ priority.cpp -g -Wall
root@u18:~/cp/test# valgrind --tool=memcheck --leak-check=yes ./a.out
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.7. and LibVEX; rerun with -h for copyright info
==== Command: ./a.out
====
自定义: q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
q3 output:
====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, , bytes allocated
====
==== All heap blocks were freed -- no leaks are possible
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )

STL之priority_queue为复合结构排序的更多相关文章

  1. STL之priority_queue(优先队列)

    priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的.它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请 ...

  2. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  3. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  4. 标准模板库(STL)学习指南之sort排序

    对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算法也基本固定下来,不需要你再去花费心思 ...

  5. STL中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...

  6. C++ STL之priority_queue

    STL中的priority_queue(优先队列)是一种会按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序的容器,不同优先级的情况下,top()上永远是最高优先级的数据,其底层采用的 ...

  7. STL 中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue>    (2)在头文件中priority_queue定义如下: nam ...

  8. STL之priority_queue

    下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...

  9. [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

随机推荐

  1. 常用Android快速开发框架

    在做项目的过程中遇到了很多困难,于是收集了一些快速开发的框架,使用后大大提高了项目开发速度,无论什么项目都可以使用的到,在此分享给大家,希望能对大家有帮助!(个人建议:有时间的同学可以看一下这些优秀框 ...

  2. NETTY 编码器介绍

    1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称为序列化(serialization),它将对象序列化为字节数组,用于网络传输.数据持久化或者其它用途. 反之,解码(Decod ...

  3. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  4. Linux 下修改Tomcat使用的JVM内存大小

    我的服务器的配置: # OS specific support.  $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...

  5. java核心技术学习笔记之一程序设计环境

    一术语 JDK:Java Delelpment Jit JRE:Java Runtime Environment 二.安装jdk1.8.0_25 设置环境变量(建议直接安装在C盘下),使用:隔开 C: ...

  6. Linux常用的系统监控shell脚本

    http://www.linuxqd.com下面是我常用的几个Linux系统监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助.1.查看主机网卡流量 #!/bin/bash #netw ...

  7. C++读写文件流的相关介绍

    掌握文本文件读写的方法了解二进制文件的读写方法 C++文件流:fstream // 文件流ifstream  // 输入文件流ofstream  // 输出文件流 //创建一个文本文件并写入信息//同 ...

  8. gcc 的include path和lib path调整

    `gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v   ------------------------------ ...

  9. VS2010/MFC对话框:消息对话框

    消息对话框 我们在使用Windows系统的过程中经常会见到消息对话框,提示我们有异常发生或提出询问等.因为在软件开发中经常用到消息对话框,所以MFC提供了两个函数可以直接生成指定风格的消息对话框,而不 ...

  10. Kate Spade_百度百科

    Kate Spade_百度百科 Kate Spade