gpt4 book ai didi

c++ - C++ 中 std::priority_queue 的比较器部分是什么意思?

转载 作者:行者123 更新时间:2023-11-28 01:27:45 25 4
gpt4 key购买 nike

C++ 中的优先级队列语法:

priority_queue <Type, vector<Type>, ComparisonType > min_heap;

如果我想声明一个最大堆,我使用std::less,否则使用std::greater(最小堆)。我不太明白为什么 std::less 导致最大堆,而 std::greater 最小堆?

最佳答案

引自 cppreference解释了这个概念:

Compare - A Compare type providing a strict weak ordering.

Note that the Compare parameter is defined such that it returns true if its first argument comes before its second argument in a weak ordering. But because the priority queue outputs largest elements first, the elements that "come before" are actually output last. That is, the front of the queue contains the "last" element according to the weak ordering imposed by Compare.

关于c++ - C++ 中 std::priority_queue 的比较器部分是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com