简体   繁体   English

为什么这个自定义比较器在构造std :: priority_queue时失败,而它适用于std :: sort?

[英]Why does this custom comparator fail in construcing std::priority_queue while it works for std::sort?

A comparator comp was defined as below. 比较器comp定义如下。 It works fine with std::sort , but fails to compile in the constructor of std::priority_queue . 它适用于std::sort ,但无法在std::priority_queue的构造函数中编译。 What is the problem? 问题是什么? Thanks. 谢谢。

#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

bool comp(int a, int b) { return a > b; }

int main()
{
    vector<int> vec = {4, 2, 1, 3};
    sort(vec.begin(), vec.end(), comp); // OK
    priority_queue<int> q1(less<int>(), vec); // OK
    priority_queue<int> q2(comp, vec); // Fail
    return 0;
}

Error message: 错误信息:

error: no matching function for call to 'std::priority_queue<int>::priority_queue(bool (&)(int, int), std::vector<int>&)'
  priority_queue<int> q2(comp, vec);
                                  ^

The type of the default comparator of std::priority_queue is std::less<T> where T is the value type. std::priority_queue的默认比较器的类型是std::less<T> ,其中T是值类型。 You are passing something of type bool(*)(int, int) instead. 您正在传递类型为bool(*)(int, int) std::sort() being a function can deduce the comparator's type. 作为函数的std::sort()可以推导出比较器的类型。 Class types can't deduce their template arguments (yet - there us discussion in the C++ committee that a future version may have class templates whose template arguments can be deduced. 类类型不能推断出它们的模板参数(但是 - 我们在C ++委员会中讨论了未来的版本可能有类模板,其模板参数可以推导出来。

You can use 您可以使用

std::priority_queue<int, std::vector<int>, bool(*)(int, int)> q(comp);

or, avoiding a hard-to-inline function pointer: 或者,避免难以内联的函数指针:

std::priority_queue<int, std::vector<int>, std::greater<int> > q;

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

相关问题 std::priority_queue 的自定义比较器背后的逻辑 - Logic behind the custom comparator for std::priority_queue std :: priority_queue:自定义排序,不定义比较器类 - std::priority_queue: Custom ordering without defining comparator class 在继承自std :: priority_queue的类中使用自定义比较器 - Use a custom comparator in a class that inherits from std::priority_queue 什么时候std :: priority_queue &lt;&gt;自行排序? - When does a std::priority_queue<> sort itself? std::map std::priority_queue &amp;&amp; lambda 比较器 - std::map std::priority_queue && lambda comparator 为什么 std::priority_queue::emplace 是对数的? - Why is std::priority_queue::emplace logarithmic? 如何为std :: priority_queue的比较器编写带有捕获的lambda - How to write lambda with captures for the comparator of std::priority_queue 为什么我需要传递一个比较器来构造一个priority_queue,当它是一个lambda时,而不是当它是std :: greater时? - Why do I need pass a comparator to construct a priority_queue when it is a lambda, but not when it is std::greater? C ++中std :: priority_queue的比较器部分的含义是什么? - What is the meaning of the comparator part of the std::priority_queue in C++? 从std :: priority_queue派生的类不使用比较器谓词/模板参数 - Class derived from std::priority_queue does'nt use comparator predicate / template parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM