目前我正在尝试实现这个解决方案: https://stackoverflow.com/a/29236236/8882282 当我使用 less<>() 时,我没有任何问题,但在其他情况下(更大的是我自己的比较器)有很多问题。 例如:
std::vector<long long int> container;
container.reserve(dimension);
std::priority_queue<long long int, std::vector<long long int>> queue(std::greater<long long int>(), std::move(container));
“没有匹配的构造函数”
你有什么想法吗?最佳答案
std::priority_queue
的默认比较器是 std::less
。您正在向构造函数传递一个 std::greater
比较器。
它们是不同的、完全不相关的类。这是你的错误。
您必须明确声明您的优先级队列,如下所示:
std::priority_queue<long long int,
std::vector<long long int>,
std::greater<long long int>>
queue(std::greater<long long int>(),
std::move(container));
关于c++ - 如何为优先级队列预分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50199746/