我知道堆是如何工作的以及它如何排列最小和最大元素。如果 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/