Comparator for min-heap in C++









up vote
20
down vote

favorite
4












I am trying to make a min-heap1 of longs in C++ using the STL make_heap, etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator:



struct greater1
bool operator()(const long& a,const long& b) const
return a>b;

;


However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped.



Is my comparator correct? what might be going wrong?



EDIT:

I realized that I am running sort_heap with no comparator, whereas when I run it this comparator, I get [15,15,9,25] from sort_heap. Now I am thinking my comparator is definitely not working, but unsure why.



1The STL makes a max-heap by default, so I need a comparator.










share|improve this question



















  • 1




    are you using the same comparator to make_heap?
    – perreal
    Dec 24 '12 at 4:07










  • I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
    – Jakob Weisblat
    Dec 24 '12 at 4:08










  • @perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
    – Jakob Weisblat
    Dec 24 '12 at 4:16











  • what do you mean by working sample code?
    – Jakob Weisblat
    Dec 24 '12 at 4:20










  • Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
    – Zéychin
    Dec 24 '12 at 4:24














up vote
20
down vote

favorite
4












I am trying to make a min-heap1 of longs in C++ using the STL make_heap, etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator:



struct greater1
bool operator()(const long& a,const long& b) const
return a>b;

;


However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped.



Is my comparator correct? what might be going wrong?



EDIT:

I realized that I am running sort_heap with no comparator, whereas when I run it this comparator, I get [15,15,9,25] from sort_heap. Now I am thinking my comparator is definitely not working, but unsure why.



1The STL makes a max-heap by default, so I need a comparator.










share|improve this question



















  • 1




    are you using the same comparator to make_heap?
    – perreal
    Dec 24 '12 at 4:07










  • I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
    – Jakob Weisblat
    Dec 24 '12 at 4:08










  • @perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
    – Jakob Weisblat
    Dec 24 '12 at 4:16











  • what do you mean by working sample code?
    – Jakob Weisblat
    Dec 24 '12 at 4:20










  • Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
    – Zéychin
    Dec 24 '12 at 4:24












up vote
20
down vote

favorite
4









up vote
20
down vote

favorite
4






4





I am trying to make a min-heap1 of longs in C++ using the STL make_heap, etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator:



struct greater1
bool operator()(const long& a,const long& b) const
return a>b;

;


However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped.



Is my comparator correct? what might be going wrong?



EDIT:

I realized that I am running sort_heap with no comparator, whereas when I run it this comparator, I get [15,15,9,25] from sort_heap. Now I am thinking my comparator is definitely not working, but unsure why.



1The STL makes a max-heap by default, so I need a comparator.










share|improve this question















I am trying to make a min-heap1 of longs in C++ using the STL make_heap, etc., but my comparator doesn't seem to be comparing properly. The following is my current comparator:



struct greater1
bool operator()(const long& a,const long& b) const
return a>b;

;


However, when I do std::pop_heap(humble.begin(),humble.end(),g); where g is an instance of greater1 and humble is a heap who makes [9,15,15,25] when sort_heap is called, I get a 15 popped.



Is my comparator correct? what might be going wrong?



EDIT:

I realized that I am running sort_heap with no comparator, whereas when I run it this comparator, I get [15,15,9,25] from sort_heap. Now I am thinking my comparator is definitely not working, but unsure why.



1The STL makes a max-heap by default, so I need a comparator.







c++ stl heap comparator min-heap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '12 at 4:37

























asked Dec 24 '12 at 4:02









Jakob Weisblat

4,10462753




4,10462753







  • 1




    are you using the same comparator to make_heap?
    – perreal
    Dec 24 '12 at 4:07










  • I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
    – Jakob Weisblat
    Dec 24 '12 at 4:08










  • @perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
    – Jakob Weisblat
    Dec 24 '12 at 4:16











  • what do you mean by working sample code?
    – Jakob Weisblat
    Dec 24 '12 at 4:20










  • Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
    – Zéychin
    Dec 24 '12 at 4:24












  • 1




    are you using the same comparator to make_heap?
    – perreal
    Dec 24 '12 at 4:07










  • I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
    – Jakob Weisblat
    Dec 24 '12 at 4:08










  • @perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
    – Jakob Weisblat
    Dec 24 '12 at 4:16











  • what do you mean by working sample code?
    – Jakob Weisblat
    Dec 24 '12 at 4:20










  • Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
    – Zéychin
    Dec 24 '12 at 4:24







1




1




are you using the same comparator to make_heap?
– perreal
Dec 24 '12 at 4:07




are you using the same comparator to make_heap?
– perreal
Dec 24 '12 at 4:07












I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
– Jakob Weisblat
Dec 24 '12 at 4:08




I use the same comparator for make_heap but just realized I might not be using it for sort_heap.
– Jakob Weisblat
Dec 24 '12 at 4:08












@perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
– Jakob Weisblat
Dec 24 '12 at 4:16





@perreal I want to make a min-heap but the STL does a max-heap if I pass it no comparator.
– Jakob Weisblat
Dec 24 '12 at 4:16













what do you mean by working sample code?
– Jakob Weisblat
Dec 24 '12 at 4:20




what do you mean by working sample code?
– Jakob Weisblat
Dec 24 '12 at 4:20












Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
– Zéychin
Dec 24 '12 at 4:24




Pardon me if I'm incorrect here, I don't work in C/C++ much, but don't const long& a and const long& b indicate pointers to constant values, and therefore comparing a to b is comparing the addresses of these values?
– Zéychin
Dec 24 '12 at 4:24












3 Answers
3






active

oldest

votes

















up vote
17
down vote



accepted










Perhaps you are missing something somewhere, the code below works as intended:



#include <vector>
#include <algorithm>
#include <iostream>

struct greater1
bool operator()(const long& a,const long& b) const
return a>b;

;

int main()
std::vector<long> humble;
humble.push_back(15);
humble.push_back(15);
humble.push_back(9);
humble.push_back(25);

std::make_heap(humble.begin(), humble.end(), greater1());
while (humble.size())
std::pop_heap(humble.begin(),humble.end(),greater1());
long min = humble.back();
humble.pop_back();
std::cout << min << std::endl;


return 0;






share|improve this answer




















  • This works for me -- I am trying to find a deviant in my code...
    – Jakob Weisblat
    Dec 24 '12 at 4:33










  • for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
    – Jakob Weisblat
    Dec 24 '12 at 4:41










  • thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
    – perreal
    Dec 24 '12 at 4:47










  • I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
    – Jakob Weisblat
    Dec 24 '12 at 4:49






  • 1




    g++ file.cpp && ./a.out
    – Joshua
    Jul 31 '16 at 23:42

















up vote
8
down vote













just use greater<int>(). it is predefined in std.






share|improve this answer





























    up vote
    1
    down vote













    You want to call make_heap on the vector again, not sort_heap. make_heap will rearrange your entire vector into a min heap given the greater-than comparator whereas sort_heap sorts your element into ascending order and is no longer a heap at all!



    #include <algorithm>
    #include <iostream>
    #include <vector>

    struct greater1
    bool operator()(const long& a,const long& b) const
    return a>b;

    ;

    int main()

    unsigned int myints = 10,20,30,5,15;
    vector<unsigned int> v(myints, myints+5);

    //creates max heap
    std::make_heap(v.begin(). v.end()); // 30 20 10 5 15

    //converts to min heap
    std::make_heap(v.begin(). v.end(), greater1()); // 5 15 10 20 30

    unsigned int s = v.size();

    //ALSO NEED TO PASS greater1() to pop()!!!
    for(unsigned int i = 0; i < s; i++)
    std::pop_heap(v.begin(). v.end(), greater1()); // popping order: 5 10 15 20 30

    return 0;






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14016921%2fcomparator-for-min-heap-in-c%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      17
      down vote



      accepted










      Perhaps you are missing something somewhere, the code below works as intended:



      #include <vector>
      #include <algorithm>
      #include <iostream>

      struct greater1
      bool operator()(const long& a,const long& b) const
      return a>b;

      ;

      int main()
      std::vector<long> humble;
      humble.push_back(15);
      humble.push_back(15);
      humble.push_back(9);
      humble.push_back(25);

      std::make_heap(humble.begin(), humble.end(), greater1());
      while (humble.size())
      std::pop_heap(humble.begin(),humble.end(),greater1());
      long min = humble.back();
      humble.pop_back();
      std::cout << min << std::endl;


      return 0;






      share|improve this answer




















      • This works for me -- I am trying to find a deviant in my code...
        – Jakob Weisblat
        Dec 24 '12 at 4:33










      • for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
        – Jakob Weisblat
        Dec 24 '12 at 4:41










      • thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
        – perreal
        Dec 24 '12 at 4:47










      • I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
        – Jakob Weisblat
        Dec 24 '12 at 4:49






      • 1




        g++ file.cpp && ./a.out
        – Joshua
        Jul 31 '16 at 23:42














      up vote
      17
      down vote



      accepted










      Perhaps you are missing something somewhere, the code below works as intended:



      #include <vector>
      #include <algorithm>
      #include <iostream>

      struct greater1
      bool operator()(const long& a,const long& b) const
      return a>b;

      ;

      int main()
      std::vector<long> humble;
      humble.push_back(15);
      humble.push_back(15);
      humble.push_back(9);
      humble.push_back(25);

      std::make_heap(humble.begin(), humble.end(), greater1());
      while (humble.size())
      std::pop_heap(humble.begin(),humble.end(),greater1());
      long min = humble.back();
      humble.pop_back();
      std::cout << min << std::endl;


      return 0;






      share|improve this answer




















      • This works for me -- I am trying to find a deviant in my code...
        – Jakob Weisblat
        Dec 24 '12 at 4:33










      • for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
        – Jakob Weisblat
        Dec 24 '12 at 4:41










      • thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
        – perreal
        Dec 24 '12 at 4:47










      • I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
        – Jakob Weisblat
        Dec 24 '12 at 4:49






      • 1




        g++ file.cpp && ./a.out
        – Joshua
        Jul 31 '16 at 23:42












      up vote
      17
      down vote



      accepted







      up vote
      17
      down vote



      accepted






      Perhaps you are missing something somewhere, the code below works as intended:



      #include <vector>
      #include <algorithm>
      #include <iostream>

      struct greater1
      bool operator()(const long& a,const long& b) const
      return a>b;

      ;

      int main()
      std::vector<long> humble;
      humble.push_back(15);
      humble.push_back(15);
      humble.push_back(9);
      humble.push_back(25);

      std::make_heap(humble.begin(), humble.end(), greater1());
      while (humble.size())
      std::pop_heap(humble.begin(),humble.end(),greater1());
      long min = humble.back();
      humble.pop_back();
      std::cout << min << std::endl;


      return 0;






      share|improve this answer












      Perhaps you are missing something somewhere, the code below works as intended:



      #include <vector>
      #include <algorithm>
      #include <iostream>

      struct greater1
      bool operator()(const long& a,const long& b) const
      return a>b;

      ;

      int main()
      std::vector<long> humble;
      humble.push_back(15);
      humble.push_back(15);
      humble.push_back(9);
      humble.push_back(25);

      std::make_heap(humble.begin(), humble.end(), greater1());
      while (humble.size())
      std::pop_heap(humble.begin(),humble.end(),greater1());
      long min = humble.back();
      humble.pop_back();
      std::cout << min << std::endl;


      return 0;







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 24 '12 at 4:28









      perreal

      71.4k9109137




      71.4k9109137











      • This works for me -- I am trying to find a deviant in my code...
        – Jakob Weisblat
        Dec 24 '12 at 4:33










      • for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
        – Jakob Weisblat
        Dec 24 '12 at 4:41










      • thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
        – perreal
        Dec 24 '12 at 4:47










      • I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
        – Jakob Weisblat
        Dec 24 '12 at 4:49






      • 1




        g++ file.cpp && ./a.out
        – Joshua
        Jul 31 '16 at 23:42
















      • This works for me -- I am trying to find a deviant in my code...
        – Jakob Weisblat
        Dec 24 '12 at 4:33










      • for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
        – Jakob Weisblat
        Dec 24 '12 at 4:41










      • thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
        – perreal
        Dec 24 '12 at 4:47










      • I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
        – Jakob Weisblat
        Dec 24 '12 at 4:49






      • 1




        g++ file.cpp && ./a.out
        – Joshua
        Jul 31 '16 at 23:42















      This works for me -- I am trying to find a deviant in my code...
      – Jakob Weisblat
      Dec 24 '12 at 4:33




      This works for me -- I am trying to find a deviant in my code...
      – Jakob Weisblat
      Dec 24 '12 at 4:33












      for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
      – Jakob Weisblat
      Dec 24 '12 at 4:41




      for some reason, after taking out and readding ",greater1()" to my calls a few times, it now works. Maybe it was generating compiler errors that I didn't notice (I have a script run g++ file.cpp;./a.out) and it finally compiled after I removed an error. Who knows. Anyway, since you were helpful to solving my problem, I'm accepting this.
      – Jakob Weisblat
      Dec 24 '12 at 4:41












      thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
      – perreal
      Dec 24 '12 at 4:47




      thanks, also maybe you mean to have the std::push_heap(humble.begin(),humble.end(),greater1()); inside the if block.
      – perreal
      Dec 24 '12 at 4:47












      I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
      – Jakob Weisblat
      Dec 24 '12 at 4:49




      I do mean to have that there. Good catch. Explains my errors for much larger values but not my newest error ( "Illegal file open (/dev/tty)")
      – Jakob Weisblat
      Dec 24 '12 at 4:49




      1




      1




      g++ file.cpp && ./a.out
      – Joshua
      Jul 31 '16 at 23:42




      g++ file.cpp && ./a.out
      – Joshua
      Jul 31 '16 at 23:42












      up vote
      8
      down vote













      just use greater<int>(). it is predefined in std.






      share|improve this answer


























        up vote
        8
        down vote













        just use greater<int>(). it is predefined in std.






        share|improve this answer
























          up vote
          8
          down vote










          up vote
          8
          down vote









          just use greater<int>(). it is predefined in std.






          share|improve this answer














          just use greater<int>(). it is predefined in std.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 7:19









          Daniel Heilper

          740927




          740927










          answered Jul 25 '13 at 7:39









          zyfo2

          10613




          10613




















              up vote
              1
              down vote













              You want to call make_heap on the vector again, not sort_heap. make_heap will rearrange your entire vector into a min heap given the greater-than comparator whereas sort_heap sorts your element into ascending order and is no longer a heap at all!



              #include <algorithm>
              #include <iostream>
              #include <vector>

              struct greater1
              bool operator()(const long& a,const long& b) const
              return a>b;

              ;

              int main()

              unsigned int myints = 10,20,30,5,15;
              vector<unsigned int> v(myints, myints+5);

              //creates max heap
              std::make_heap(v.begin(). v.end()); // 30 20 10 5 15

              //converts to min heap
              std::make_heap(v.begin(). v.end(), greater1()); // 5 15 10 20 30

              unsigned int s = v.size();

              //ALSO NEED TO PASS greater1() to pop()!!!
              for(unsigned int i = 0; i < s; i++)
              std::pop_heap(v.begin(). v.end(), greater1()); // popping order: 5 10 15 20 30

              return 0;






              share|improve this answer


























                up vote
                1
                down vote













                You want to call make_heap on the vector again, not sort_heap. make_heap will rearrange your entire vector into a min heap given the greater-than comparator whereas sort_heap sorts your element into ascending order and is no longer a heap at all!



                #include <algorithm>
                #include <iostream>
                #include <vector>

                struct greater1
                bool operator()(const long& a,const long& b) const
                return a>b;

                ;

                int main()

                unsigned int myints = 10,20,30,5,15;
                vector<unsigned int> v(myints, myints+5);

                //creates max heap
                std::make_heap(v.begin(). v.end()); // 30 20 10 5 15

                //converts to min heap
                std::make_heap(v.begin(). v.end(), greater1()); // 5 15 10 20 30

                unsigned int s = v.size();

                //ALSO NEED TO PASS greater1() to pop()!!!
                for(unsigned int i = 0; i < s; i++)
                std::pop_heap(v.begin(). v.end(), greater1()); // popping order: 5 10 15 20 30

                return 0;






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You want to call make_heap on the vector again, not sort_heap. make_heap will rearrange your entire vector into a min heap given the greater-than comparator whereas sort_heap sorts your element into ascending order and is no longer a heap at all!



                  #include <algorithm>
                  #include <iostream>
                  #include <vector>

                  struct greater1
                  bool operator()(const long& a,const long& b) const
                  return a>b;

                  ;

                  int main()

                  unsigned int myints = 10,20,30,5,15;
                  vector<unsigned int> v(myints, myints+5);

                  //creates max heap
                  std::make_heap(v.begin(). v.end()); // 30 20 10 5 15

                  //converts to min heap
                  std::make_heap(v.begin(). v.end(), greater1()); // 5 15 10 20 30

                  unsigned int s = v.size();

                  //ALSO NEED TO PASS greater1() to pop()!!!
                  for(unsigned int i = 0; i < s; i++)
                  std::pop_heap(v.begin(). v.end(), greater1()); // popping order: 5 10 15 20 30

                  return 0;






                  share|improve this answer














                  You want to call make_heap on the vector again, not sort_heap. make_heap will rearrange your entire vector into a min heap given the greater-than comparator whereas sort_heap sorts your element into ascending order and is no longer a heap at all!



                  #include <algorithm>
                  #include <iostream>
                  #include <vector>

                  struct greater1
                  bool operator()(const long& a,const long& b) const
                  return a>b;

                  ;

                  int main()

                  unsigned int myints = 10,20,30,5,15;
                  vector<unsigned int> v(myints, myints+5);

                  //creates max heap
                  std::make_heap(v.begin(). v.end()); // 30 20 10 5 15

                  //converts to min heap
                  std::make_heap(v.begin(). v.end(), greater1()); // 5 15 10 20 30

                  unsigned int s = v.size();

                  //ALSO NEED TO PASS greater1() to pop()!!!
                  for(unsigned int i = 0; i < s; i++)
                  std::pop_heap(v.begin(). v.end(), greater1()); // popping order: 5 10 15 20 30

                  return 0;







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 1 '16 at 0:41

























                  answered Jul 31 '16 at 23:25









                  Dobob

                  8718




                  8718



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14016921%2fcomparator-for-min-heap-in-c%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto