Comparator for min-heap in C++
up vote
20
down vote
favorite
I am trying to make a min-heap1 of long
s 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
|
show 2 more comments
up vote
20
down vote
favorite
I am trying to make a min-heap1 of long
s 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
1
are you using the same comparator tomake_heap
?
– perreal
Dec 24 '12 at 4:07
I use the same comparator formake_heap
but just realized I might not be using it forsort_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'tconst long& a
andconst long& b
indicate pointers to constant values, and therefore comparinga
tob
is comparing the addresses of these values?
– Zéychin
Dec 24 '12 at 4:24
|
show 2 more comments
up vote
20
down vote
favorite
up vote
20
down vote
favorite
I am trying to make a min-heap1 of long
s 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
I am trying to make a min-heap1 of long
s 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
c++ stl heap comparator min-heap
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 tomake_heap
?
– perreal
Dec 24 '12 at 4:07
I use the same comparator formake_heap
but just realized I might not be using it forsort_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'tconst long& a
andconst long& b
indicate pointers to constant values, and therefore comparinga
tob
is comparing the addresses of these values?
– Zéychin
Dec 24 '12 at 4:24
|
show 2 more comments
1
are you using the same comparator tomake_heap
?
– perreal
Dec 24 '12 at 4:07
I use the same comparator formake_heap
but just realized I might not be using it forsort_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'tconst long& a
andconst long& b
indicate pointers to constant values, and therefore comparinga
tob
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
|
show 2 more comments
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;
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 rung++ 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 thestd::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
add a comment |
up vote
8
down vote
just use greater<int>()
. it is predefined in std.
add a comment |
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;
add a comment |
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;
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 rung++ 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 thestd::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
add a comment |
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;
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 rung++ 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 thestd::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
add a comment |
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;
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;
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 rung++ 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 thestd::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
add a comment |
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 rung++ 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 thestd::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
add a comment |
up vote
8
down vote
just use greater<int>()
. it is predefined in std.
add a comment |
up vote
8
down vote
just use greater<int>()
. it is predefined in std.
add a comment |
up vote
8
down vote
up vote
8
down vote
just use greater<int>()
. it is predefined in std.
just use greater<int>()
. it is predefined in std.
edited Nov 11 at 7:19
Daniel Heilper
740927
740927
answered Jul 25 '13 at 7:39
zyfo2
10613
10613
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
edited Aug 1 '16 at 0:41
answered Jul 31 '16 at 23:25
Dobob
8718
8718
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 forsort_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
andconst long& b
indicate pointers to constant values, and therefore comparinga
tob
is comparing the addresses of these values?– Zéychin
Dec 24 '12 at 4:24