c++ - 为什么在优先级队列中使用容器参数进行重载?

标签 c++ c++11 stl c++14 priority-queue

std::priority_queue< int, std::vector<int>, std::greater<int> > pq;

为什么要使用vector参数?

最佳答案

  • 优先级队列实现可以使用不同类型的容器来构建底层数据结构。您可以使用该模板参数指定您最喜欢哪一个。

  • 引自 here

    Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer, and its iterators must satisfy the requirements of RandomAccessIterator. Additionally, it must provide the following functions with the usual semantics: front() push_back() pop_back() The standard containers std::vector and std::deque satisfy these requirements.

顺便说一句,vector 是默认的。

关于c++ - 为什么在优先级队列中使用容器参数进行重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44995031/

相关文章:

c++ - 数组和指针算术混淆

c++ - 使用 C++ 操作 protobuf 中的数据结构

C++ const char* 重载困惑

c++ - Qt并发: call member function from another class

c++ - STL 容器上的 lambda+for_each+delete

c++ - 临时变量被重载的 + 和 += 运算符破坏

c++11 - std::move-如何警告程序员不要使用* move 自*对象

c++ - 提供方法指针和派生类型对象时 std::bind 的一致性

c++ - DirectX 中的 vector 处理项目符号

c++ - 如何将 std::vector 插入具有自定义排序功能的 std::set