0

Hello I have the following code:

#include <algorithm>
#include <queue>
#include <functional>
std::priority_queue < std::pair<int, int>, std::greater<std::pair<int, int> > > q;

I am trying to use the min heap functionality. I have search for a couple of hours now still cannot get a clear answer. I have seen a lot of people write custom comparison functions but I think it is pretty standard operation. If I take out the std::greater func everything works as expected but it creates a max heap.

I get 14 errors when I compile this.

Error   C2039   'value_type': is not a member of 'std::greater<std::pair<int,int>>' 
Error   C2146   syntax error: missing '>' before identifier 'value_type'    
Error   C2039   'value_type': is not a member of 'std::greater<std::pair<int,int>>' 
Error   C3646   'value_type': unknown override specifier    
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'size_type': is not a member of 'std::greater<std::pair<int,int>>'  
Error   C3646   'size_type': unknown override specifier     
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'reference': is not a member of 'std::greater<std::pair<int,int>>'  
Error   C3646   'reference': unknown override specifier
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'const_reference': is not a member of 'std::greater<std::pair<int,int>>'    
Error   C3646   'const_reference': unknown override specifier   
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int
1
  • 1
    Did you look at the documentation of std::priority_queue? You are trying to pass std::greater as a Container, not as a comparator. Commented Feb 7, 2017 at 21:14

1 Answer 1

3

The second template parameter to priority_queue is the underlying container type to use, not the comparison operator to use. It defaults to std::vector<T>, which you can specify explicitly:

typedef std::pair<int, int> QueueItem;
std::priority_queue <QueueItem, std::vector<QueueItem>, std::greater<QueueItem> > q;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much Mark. It was it.
Write using QueueItem = std::pair<int, int>; better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.