【问题标题】:How to preallocate memory for priority queue?如何为优先队列预分配内存?
【发布时间】:2018-05-06 12:33:41
【问题描述】:

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

“没有匹配的构造函数”

你有什么想法吗?

【问题讨论】:

    标签: c++ constructor stl comparator priority-queue


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2021-08-31
      相关资源
      最近更新 更多