[英]Can't use std::greater where std::less is used
This code compiles just fine:这段代码编译得很好:
#include <queue>
#include <functional>
std::priority_queue<int> q (std::less<int>{});
whereas this code gives weird compilation errors而这段代码给出了奇怪的编译错误
#include <queue>
#include <functional>
std::priority_queue<int> q (std::greater<int>{});
The 3rd template argument for std::priority_queue
is std::less
by default. std::priority_queue
的第三个模板参数默认为std::less
。 So the constructor you are calling is expecting a std::less
object.因此,您正在调用的构造函数需要一个
std::less
object。 You have to change the 3rd template argument:您必须更改第三个模板参数:
std::priority_queue<int, std::vector<T>, std::greater<int>> q (std::greater<int>{});
Passing in std::greater<int>{}
is also unneeded as there's a default constructor to handle that for you.也不需要传入
std::greater<int>{}
,因为有一个默认构造函数可以为您处理。
std::priority_queue<int, std::vector<T>, std::greater<int>> q;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.