zoukankan      html  css  js  c++  java
  • C++ priority_queue的使用 & Java PriorityQueue

    刚刚那道BST的题目,也用到了priority_queue,那是那个没有定义比较函数。

    那么下面这个,就要定义比较函数。

    它的模板声明带有三个参数,priority_queue<Type, Container, Functional>

    struct cmp{
        bool operator() ( Node a, Node b ){
            if( a.x== b.x ) return a.y> b.y;
            
            return a.x> b.x; }
    };


        priority_queue<Node, vector<Node>, cmp> q;

    或者重载比较符号,也可以:

    struct Node{
        int x, y;
        Node( int a= 0, int b= 0 ):
            x(a), y(b) {}
    };

    bool operator<( Node a, Node b ){
        if( a.x== b.x ) return a.y> b.y;
        return a.x> b.x; 
    }

    就不需要在类型里面定义了。

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

    struct Node{
        int x, y;
        Node( int a= 0, int b= 0 ):
            x(a), y(b) {}

        friend operator<( Node a, Node b ){
        if( a.x== b.x ) return a.y> b.y;
        return a.x> b.x;

        }
    };

    http://blog.csdn.net/sraing/article/details/6256302

    Java

    PriorityQueue<Cell> queue = new PriorityQueue<>(1, new Comparator<Cell>(){
                public int compare(Cell a, Cell b) {
                    return a.height - b.height;
                }
            });
  • 相关阅读:
    golang版本管理工具gvm
    golang问题101
    服务器部署Jupyter Notebook
    Python 获取MySql某个表所有字段名
    Python自带HTTP文件传输服务
    log4net 配置
    GridView控件RowDataBound事件中获取列字段值的几种途径
    最简单去Button回车事件
    oracle 多条执行语句同时执行
    MERGE Into
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6446518.html
Copyright © 2011-2022 走看看