C++ STL--以 pair<int,string> 为数据类型的 make_heap

标签 c++ heap

我知道堆是如何工作的以及它如何排列最小和最大元素。如果 vector 仅包含 int,则很容易在 STL 中应用 make_heap。但是如果 vector 包含字符串和整数结构,如何应用make_heap()。我想根据结构中的 int 值制作堆。 请告诉我该怎么做。

最佳答案

你必须为你的结构提供比较功能:

struct A
{
 int x, y;
};

struct Comp
{
   bool operator()(const A& s1, const A& s2)
   {
       return s1.x < s2.x && s1.y == s2.y;
   }
};

std::vector<A> vec;
std::make_heap(vec.begin(), vec.end(), Comp());

关于C++ STL--以 pair<int,string> 为数据类型的 make_heap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13190269/

相关文章:

algorithm - 堆 - 对未知输入执行 DeleteMax 操作 - 实现

c++ - 是否可以向系统库 header 添加空格?

c++ - 在 C++ 中类应该以什么顺序声明?

c++ - VS 2012 MFC 对话框中缺少 WM_WINDOWPOSCHANGING

c++ - 什么是静态变量?

Python : Functional code speed is faster than pure code speed. 为什么?

python - K-最大元素算法比较

c++ - 使用静态 vector 时 _Orphan_range 崩溃

电子交易所top K股算法

c++ - 带有 MoveConstructible 对象的 std::push_heap 和 std::pop_heap