zoukankan      html  css  js  c++  java
  • STL里的priority_queue用法

    在STL里有这个priority_queue,实现优先队列的结构。在优先队列中,优先级高的元素先出队列。

    现在在这里说说用法吧

    先看看语法:

    Syntax:

    In their implementation in the C++ Standard Template Library, priority queues take three template parameters:1
    2 template < class T, class Container = vector,
    class Compare = less > class priority_queue;

    Where the template parameters have the following meanings:
    T: Type of the elements.
    Container: Type of the underlying container object used to store and access the elements.
    Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less, which returns the same as applying the less-than operator (a<b).
    The priority_queue object uses this expression when an element is inserted or removed from it (using push or pop, respectively) to grant that the element popped is always the greater in the priority queue.

    可以自定义一个比较类,Compare

    其实就三种用法

    第一种,直接使用默认的。

    它的模板声明带有三个参数,priority_queue<type, container,="" functional="">
    Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
    Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
    STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个
    参数缺省的话,优先队列就是大顶堆,队头元素最大。
    看例子

    1. priority_queue<int> qi;
    2. int a[len] = {3,5,9,6,2};
    3. priority_queue<int> qi;
    4. for(i = 0; i < len; i++)
    5. qi.push(a[i]);
    6. for(i = 0; i < len; i++)
    7. {
    8. cout<<qi.top()<<" ";
    9. qi.pop();
    10. }

    通过<操作符可知在整数中元素大的优先级高。
    故例子中输出结果为:9 6 5 3 2

    第二种方法:实现比较函数 重载operator<

    在示例1中,如果我们要把元素从小到大输出怎么办呢?
    这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

    如果要用到小顶堆,则一般要把模板的三个参数都带进去。
    STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
    priority_queue<int, vector<int="">, greater >qi2;

    对于自定义类型,则必须自己重载 operator< 或者自己写仿函数

    1. #include <iostream>
    2. #include <queue>
    3. using namespace std;
    4. struct Node{
    5. int x, y;
    6. Node( int a= 0, int b= 0 ):
    7. x(a), y(b) {}
    8. };
    9. bool operator<( Node a, Node b ){
    10. if( a.x== b.x ) return a.y> b.y;
    11. return a.x> b.x;
    12. }
    13. int main(){
    14. priority_queue<Node> q;
    15. for( int i= 0; i< 10; ++i )
    16. q.push( Node( rand(), rand() ) );
    17. while( !q.empty() ){
    18. cout << q.top().x << ' ' << q.top().y << endl;
    19. q.pop();
    20. }
    21. getchar();
    22. return 0;
    23. }

    或者这样定义也是能达到效果的:

    1. struct Node{
    2. int x, y;
    3. Node( int a= 0, int b= 0 ):
    4. x(a), y(b) {}
    5. friend operator<( Node a, Node b ){
    6. if( a.x== b.x ) return a.y> b.y;
    7. return a.x> b.x;
    8. }
    9. };

    第三种:实现比较仿函数

    自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
    但此时不能像基本类型这样声明
    priority_queue<node, vector<node="">, greater >;
    原因是 greater 没有定义,如果想用这种方法定义
    则可以按如下方式

    例子:

    1. #include <iostream>
    2. #include <queue>
    3. using namespace std;
    4. struct Node{
    5. int x, y;
    6. Node( int a= 0, int b= 0 ):
    7. x(a), y(b) {}
    8. };
    9. struct cmp{
    10. bool operator() ( Node a, Node b ){
    11. if( a.x== b.x ) return a.y> b.y;
    12. return a.x> b.x; }
    13. };
    14. int main(){
    15. priority_queue<Node, vector<Node>, cmp> q;
    16. for( int i= 0; i< 10; ++i )
    17. q.push( Node( rand(), rand() ) );
    18. while( !q.empty() ){
    19. cout << q.top().x << ' ' << q.top().y << endl;
    20. q.pop();
    21. }
    22. getchar();
    23. return 0;
    24. }

    还有一点要注意的是priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果,有第三个参数的话,必定要写第二个参数。

  • 相关阅读:
    mongodb 3.4复制搭建
    mongodb 用户管理
    mongodb 3.4 TAR包启动多个实例
    mongodb 3.4 YUM安装
    mongodb数据库备份恢复-windows系统
    mongodb数据库索引管理
    mongodb数据库集合操作
    Unity3d 实现鼠标左键点击地形使角色移动到指定地点[脚本]
    Unity3D性能优化之Draw Call Batching
    Unity3D占用内存太大怎么解决呢?
  • 原文地址:https://www.cnblogs.com/shihuvini/p/10897390.html
Copyright © 2011-2022 走看看