Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL--make_heap with pair<int,string> as data type

Tags:

c++

heap

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.

like image 813
username_4567 Avatar asked Dec 05 '22 13:12

username_4567


1 Answers

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());
like image 65
Denis Ermolin Avatar answered Dec 24 '22 02:12

Denis Ermolin