C ++ STL--make_heap与pair< int,string>作为数据类型 [英] C++ STL--make_heap with pair<int,string> as data type

查看:96
本文介绍了C ++ STL--make_heap与pair< int,string>作为数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道堆如何工作以及如何安排最小和最大元素.如果vector仅包含int,则很容易在STL中应用make_heap.但是如果vector包含string和int的结构,如何应用make_heap().想要基于结构中的int值来生成堆. 请告诉我该怎么做.

I know how does heap work and how it arranges min and max elements. It is easy, if vector contains only int, to apply make_heap in STL. But how to apply make_heap() if vector contains structure of string and int. Iwant to make heap based on int value in structure. Please tell me how to do that.

推荐答案

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

You have to provide comparison function for your structure:

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--make_heap与pair&lt; int,string&gt;作为数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆