如何为优先队列预分配内存?

How to preallocate memory for priority queue?

目前我正在尝试实施此解决方案: 当我使用 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));

"No matching constructor"

你有什么想法吗?

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));