Illia
Illia

Reputation: 301

std::priority_queue with std::pair<int, int>

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

Upvotes: 0

Views: 3155

Answers (1)

Mark B
Mark B

Reputation: 96243

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;

Upvotes: 3

Related Questions