Std :: priority_queue: Custom order without defining a comparator class

I want to have a priority queue with a custom order, but lazy, like me, I don’t want to define the implementation class of the comparator () class.

I would like something like this to compile:

std::priority_queue<int, std::vector<int>, boost::bind(some_function, _1, _2, obj1, obj2)> queue; 

where some_function is a bool return function that takes four arguments, the first and second are the ints of the queue, and the last two are some of the objects needed to calculate the ordering (const references).

(error: "boost :: bind cannot be displayed in constant expression)

But this does not compile. Even simpler

 std::priority_queue<int, std::vector<int>, &compare> queue; 

will not compile, and the comparison will be a binary function that returns bool.

(error: type / value mismatch in argument 3 in the template parameter list for 'template class std :: priority_queue; expected type obtained by' compare)

Any suggestions?

+4
source share
1 answer

This might work:

 std::priority_queue<int, std::vector<int>, boost::function<bool(int,int)> > 

Then pass the binding expression to the queue constructor.

PS you get these compilation errors because you put expressions evaluated at runtime where a type name or constant is expected.

+11
source

All Articles