Karan Singh
Karan Singh

Reputation: 1164

Priority Queue Implementation in STL

I was looking at a piece of code, where I came across:

priority_queue<trainer, vector<trainer>, function<bool(trainer,trainer)> > p(comp);

here, trainer is a user-defined struct and comp is a comparator function. I am having trouble understanding what the 3rd argument function<bool(trainer,trainer)> mean and why is the comparator function in parantheses.

From the documentation: Isn't the third parameter supposed to contain the comparator and the parantheses is supposed to pass a range of something?

Upvotes: 0

Views: 22

Answers (1)

Marshall Clow
Marshall Clow

Reputation: 16670

priority_queue has three template parameters. A type, a container, and a comparison function.

In your example, the type is trainer, the container is vector<trainer>, and the type of the comparison object is function<bool(trainer,trainer)>.

When you instantiate one of these things, you need to pass it a specific comparison function, and in this case that is comp.

If comp is just a function pointer, then it will be (implicitly) converted to a std::function<bool(trainer, trainer)> object, which will be used by the priority_queue

Upvotes: 1

Related Questions