【问题标题】:Can we override the functions of C++ STL containers?我们可以覆盖 C++ STL 容器的功能吗?
【发布时间】:2020-04-14 20:32:56
【问题描述】:

例如,我想通过 priority_queue 来使用 Min Heap,而不是删除最小的元素,我想覆盖 pop() 函数每次从最小堆中删除一个特定元素

【问题讨论】:

  • 您每次要删除哪个特定元素?

标签: oop stl c++14


【解决方案1】:

没有。

std::priority_queue::pop 不是一个虚函数,因此即使您确实对它进行了子类化并编写了一个新函数,也不会从 std::priority_queue &std::priority_queue * 调用它。

如果您想要一个最大堆,您可以将std::priority_queue 的默认参数之一从std::less 更改为std::greater,例如

template <typename T>
using max_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多