简体   繁体   English

如何解释优先级队列的模板签名?

[英]How do I interpret this template signature of priority queue?

template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;    

I understand the first two template arguments, the first template argument is the data type of the element being stored in the priority queue and the second one is the kind of container that the programmer wants to use, it could be either deque or vector. 我了解前两个模板参数,第一个模板参数是存储在优先级队列中的元素的数据类型,第二个模板是程序员想要使用的容器类型,它可以是双端队列或向量。

But the third argument confuses me a bit, because I have never seen something like it. 但是第三个论点使我有些困惑,因为我从未见过类似的东西。 I'd have done something like: 我会做类似的事情:

template <class T, class Container = vector<T>
class priority_queue{
    /* Implementation */
};

Does it have something to do with the strict weak ordering criterion necessary for priority queue? 它与优先级队列所必需的严格的弱排序标准有关吗? If yes, how can I learn more about it? 如果是,我如何了解更多信息? Could you give an example of using the third argument? 您能举一个使用第三个参数的例子吗?

I am new to template programming, so I'd really appreciate your help. 我是模板编程的新手,非常感谢您的帮助。

The third parameter specifies the comparator class. 第三个参数指定比较器类。

The comparator class is responsible for comparing queue elements, in order to determine the queue order. 比较器类负责比较队列元素,以确定队列顺序。 You already understand that the elements in the queue are ordered with the "higher" values first. 您已经了解到队列中的元素首先按“较高”值排序。 Well, this is what defines what "higher" means, here. 好吧,这就是定义“更高”的含义的地方。

The comparator class has a simple interface: given two values, return true if the first value is less than the second value, and false otherwise. 比较器类具有一个简单的接口:给定两个值,如果第一个值小于第二个值,则返回true否则返回false The default implementation, std::less , uses the traditional < operator to compare the two values. 默认实现std::less使用传统的<运算符比较两个值。

Use a custom comparator class in order to change the behavior of the priority queue. 使用自定义比较器类以更改优先级队列的行为。 One example would be to specify std::greater instead of std::less as the comparator class. 一个示例是将std::greater而不是std::less为比较器类。 std::greater uses the > operator, so this creates a priority queue "in opposite order", which gives you the lowest values first, rather than highest one. std::greater使用>运算符,因此这会创建一个“相反顺序”的优先级队列,该队列首先提供最低值,而不是最高值。

Or, you could create your own custom comparator class, such as: 或者,您可以创建自己的自定义比较器类,例如:

class last_four_bits {

public:

    bool operator()(int a, int b) const
    {
           return (a & 0x0F) < (b & 0x0F);
    }
};

This comparator class compares the least four bits of an int only. 该比较器类仅比较int的最低四位。 This, in turn, makes this: 反过来,这使得:

std::priority_queue<int, std::vector<int>, last_four_bits>

look at the least four bits of each int value in the queue, thus ordering all int s with the highest values in the last four bits before the ones with the lesser values, ignoring all other bits in the int . 查看队列中每个int值的至少四位,因此将最后四位中具有最高值的所有int排序,将其值较小前的所有int排序,而忽略int中的所有其他位。

PS Comparator classes are also used with associative containers, set s and map s, and serve the same function there. PS Comparator类也与关联容器, setmap ,并在其中提供相同的功能。 By carefully crafting a comparator class you can create sets and maps whose iterators iterate over the keys in the set/map in some order other than the lowest to the highest keys (as you understand "lowest" and "highest" to mean, intrinsically). 通过精心设计比较器类,您可以创建集合和映射,其迭代器以某种顺序遍历集合/映射中的键,而不是从最低到最高(从本质上来说,这意味着“最低”和“最高”) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM