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;
                }
            });
  • 相关阅读:
    元素类型
    PS
    盒模型
    文本样式
    WebStorm、IntelliJ IDEA、JetBrains、PhpStorm、RubyMine、PyCharm
    微信iOS WKWebview 网页开发适配指南
    js取url参数
    windows 80 端口占用
    前端Demo常用库文件链接
    MAC下利用Github 、hexo、 多说、百度统计 建立个人博客指南
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6446518.html
Copyright © 2011-2022 走看看