第九章 堆

如何写堆的比较函数:

1、指针函数

  1. typedef一个函数指针
  2. 定义一个实际的bool比较函数(比较内容)
  3. 定义priority_queue的时候,在参数名后面放入函数名;
typedef bool (*costcomp)(connection a, connection b);
bool costCompare(connection a, connection b)
{
    return a.cost > b.cost;
}

typedef bool (*namecomp)(connection a, connection b);
bool nameCompare(connection a, connection b)
{
    if(a.a != b.a)
    {
        return a.a > b.a;
    }
    else
    {
        return a.b > b.b;
    }
}

vector<connection> mst(vector<connection>& vec)
{
    vector<connection> ret;
    if(vec.empty())
    {
        return ret;
    }

    priority_queue<connection, vector<connection>, costcomp> heap(costCompare);

    priority_queue<connection, vector<connection>, namecomp> heap2(nameCompare);

    unionfind uf;
}

二、比较类

results matching ""

    No results matching ""