C++ 优先队列比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20826078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Priority Queue Comparison
提问by user2455103
I'm trying to declare a priority queue in c++ using a custom comparison function...
我正在尝试使用自定义比较函数在 C++ 中声明一个优先级队列......
So , I declare the queue as follows:
所以,我声明队列如下:
std::priority_queue<int,std::vector<int>, compare> pq;
and here's the compare function :
这是比较功能:
bool compare(int a, int b)
{
return (a<b);
}
I'm pretty sure I did this before, without a class,in a similar way, but now, this code doesn't compile and I get several errors like this :
我很确定我以前在没有类的情况下以类似的方式这样做过,但是现在,此代码无法编译,并且出现以下几个错误:
type/value mismatch at argument 3 in template parameter list for 'template<class _Tp, class _Sequence, class _Compare> class std::priority_queue'
Is there a way to create a compare function similar to this but without using a class?
有没有办法创建与此类似但不使用类的比较函数?
Thanks
谢谢
采纳答案by leemes
The template parameter should be the typeof the comparison function. The function is then either default-constructed or you pass a function in the constructor of priority_queue
. So try either
模板参数应该是比较函数的类型。然后该函数要么是默认构造的,要么是您在 的构造函数中传递一个函数priority_queue
。所以尝试
std::priority_queue<int, std::vector<int>, decltype(&compare)> pq(&compare);
or don't use function pointers but instead a functor from the standard library which then can be default-constructed, eliminating the need of passing an instance in the constructor:
或者不使用函数指针,而是使用标准库中的函子,然后可以默认构造函数,从而无需在构造函数中传递实例:
std::priority_queue<int, std::vector<int>, std::less<int> > pq;
If your comparison function can't be expressed using standard library functors (in case you use custom classes in the priority queue), I recommend writing a custom functor class, or use a lambda.
如果您的比较函数无法使用标准库函子表达(以防您在优先级队列中使用自定义类),我建议编写自定义函子类,或使用 lambda。
回答by Jaa-c
You can use C++11 lambdafunction. You need to create lambda object, pass it to the template using decltype
and also pass it to the constructor. It looks like this:
您可以使用 C++11 lambda函数。您需要创建 lambda 对象,将其传递给模板 usingdecltype
并将其传递给构造函数。它看起来像这样:
auto comp = [] (int &a, int &b) -> bool { return a < b; };
std::priority_queue<int,std::vector<int>, decltype(comp) > pq (comp);
回答by 4pie0
you have to specify function type and instantiate the function in priority_queue
constructor.
您必须指定函数类型并在priority_queue
构造函数中实例化该函数。
#include <functional>
bool compare(int a, int b)
{
return (a<b);
}
std::priority_queue<int, std::vector<int>,
std::function<bool(int, int)>> pq(compare);
回答by brack
std::priority_queue<int, std::vector<int>, bool (*)compare(int, int)> pq(compare);
Is another way not mentioned.
是另一种没有提到的方式。
回答by Emil Condrea
You can use a typedef. This compiles very well:
您可以使用 typedef。这编译得很好:
typedef bool (*comp)(int,int);
bool compare(int a, int b)
{
return (a<b);
}
int main()
{
std::priority_queue<int,std::vector<int>, comp> pq(compare);
return 0;
}
回答by tushar pahuja
This worked perfectly for me.
这对我来说非常有效。
struct compare{
bool operator() (const int& p1,const int& p2 ){
return p1<p2;
}
};
int main(){
priority_queue<int,vector<int>, compare > qu;
return 0;
}