Malvika Agrawal
Malvika Agrawal

Reputation: 45

Doubt in STL priority_queue instantiation

In the code base I'm maintaining, I find the following instantiation of a STL priority_queue. I didn't understand the pq(order) part. What could be order there in the context of priority_queue instantiation?

priority_queue<Record*, vector<Record*>, Comparator > pq(order);

EDIT: Could order be the argument to the Comparator constructor? In fact, order is not an instance of type Comparator. Comparator class has a constructor that takes argument of the type of order. But I don't see how it fits in the syntax.

Upvotes: 2

Views: 262

Answers (4)

JUST MY correct OPINION
JUST MY correct OPINION

Reputation: 36107

order is the argument to the constructor of the priority_queue<Record*, vector<Record*>, Comparator> instance named pq. If you look at the documentation for the constructor you'll see that order is the Comparator instance used in the instance.

Upvotes: 1

wilhelmtell
wilhelmtell

Reputation: 58677

order is an instance of the Comparator class. It defines the comparison mechanism of the priority queue, to determine which element will be at the top.

Take a look at the std::priority_queue reference, and the constructor signature in particular.

Upvotes: 1

Rup
Rup

Reputation: 34408

Assuming that's a variable instantiation of pq, of type priority_queue<Record*, vector<Record*>, Comparator >, then order is the constructor argument. In this case it's using the explicit constructor:

 explicit priority_queue ( const Compare& x = Compare(),
                           const Container& y = Container() );

and so order will be an existing object of the Compare template parameter case - Comparator in your case - that is used for ordering.

Upvotes: 2

UncleBens
UncleBens

Reputation: 41331

From the looks of it, the code is using the following constructor of priority_queue:

explicit priority_queue ( const Compare& x = Compare(), const Container& y = Container());

If so, then order is an instance of type Comparator.

If it has been constructed with the default constructor, then you could just leave it out and call

priority_queue<Record*, vector<Record*>, Comparator > pq;

Upvotes: 2

Related Questions