Data structures 优先级队列的优先级始终需要为整数?

Data structures 优先级队列的优先级始终需要为整数?,data-structures,priority-queue,Data Structures,Priority Queue,我只是好奇我是否可以有任何其他数据类型给予优先权?像字符串、浮点数等?否 优先级队列的排序元素不必是整数 对 您可以使用您想要的任何类型,只要可以比较该类型的两个值以确定其固有顺序 基本上,您可以构建一个优先级队列,该队列使用您想要的任何类型,甚至是一个复数,前提是您可以确定对这些类型有意义的顺序 然而,这里还有一个未被问到的问题,答案是: 是的,优先级队列的大多数现有实现将使用整数作为排序元素,因为这是用于此目的的最简单、最常见的值。如果可以相互比较该类型的值,则可以使用任何类型作为优先级。在

我只是好奇我是否可以有任何其他数据类型给予优先权?像字符串、浮点数等?

优先级队列的排序元素不必是整数

您可以使用您想要的任何类型,只要可以比较该类型的两个值以确定其固有顺序

基本上,您可以构建一个优先级队列,该队列使用您想要的任何类型,甚至是一个复数,前提是您可以确定对这些类型有意义的顺序

然而,这里还有一个未被问到的问题,答案是:


是的,优先级队列的大多数现有实现将使用整数作为排序元素,因为这是用于此目的的最简单、最常见的值。

如果可以相互比较该类型的值,则可以使用任何类型作为优先级。

在摘要中,任何具有合理优先级的类型都可以用作优先级队列中的优先级。您使用的语言将决定如何定义这个排序:在C++中,运算符<在标准容器中使用,在java中,接口可比较使用,函数通常使用。自定义比较函数也经常被支持,它可以以不同于默认的方式比较元素。

< P>这里是一个完整的C++如何演示SillyJobs的定义,定义为

struct SillyJob
{
    std::string description;
    std::string priority;
    // ...
};

它有两种方式:使用成员
运算符或不指定语言?答案是响亮的“是”。您可以做任何想做的事情,也可以使用任何其他类型(只要它满足底层存储容器的容器要求)。仅,然后需要在构造时指定排序谓词。请参阅我关于如何使用这两种方法的答案。(嗯,刚刚意识到我假设C++,但是,大多数其他平台库都有类似的机制来指定比较器,例如.NET中)。
Silly: (by description length)
LOW: very very long description
HIGH: short
------------------------------------------------------------
Not so silly: (by priority value)
HIGH: short
LOW: very very long description
#include <queue>
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <map>

struct SillyJob
{
    std::string description;
    std::string priority;

    SillyJob(const std::string& d, const std::string& p)
        : description(d), priority(p) { }

    bool operator<(const SillyJob& sj) const { return description.size() < sj.description.size(); }

    friend std::ostream& operator<<(std::ostream& os, const SillyJob& sj)
    { return os << sj.priority << ": " << sj.description; }
};

static bool by_priority(const SillyJob& a, const SillyJob& b)
{
    static std::map<std::string, int> prio_map;
    if (prio_map.empty())
    {
        prio_map["HIGH"]   = 3;
        prio_map["MEDIUM"] = 2;
        prio_map["LOW"]    = 1;
    }

    return prio_map[a.priority] < prio_map[b.priority];
}

int main()
{
    std::cout << "Silly: (by description length)" << std::endl;
    {
        // by description length (member operator<)
        std::priority_queue<SillyJob> silly_queue;

        silly_queue.push(SillyJob("short", "HIGH"));
        silly_queue.push(SillyJob("very very long description", "LOW"));

        while (!silly_queue.empty())
        {
            std::cout << silly_queue.top() << std::endl;
            silly_queue.pop();
        }
    }

    std::cout << std::string(60, '-') << "\nNot so silly: (by priority value)" << std::endl;
    {
        // by description length (member operator<)
        typedef bool (*cmpf)(const SillyJob&, const SillyJob&);
        typedef std::priority_queue<SillyJob, std::vector<SillyJob>, cmpf> not_so_silly_queue;

        not_so_silly_queue queue(by_priority);

        queue.push(SillyJob("short", "HIGH"));
        queue.push(SillyJob("very very long description", "LOW"));

        while (!queue.empty())
        {
            std::cout << queue.top() << std::endl;
            queue.pop();
        }
    }

}