开发者

Doubt in STL priority_queue instantiation

开发者 https://www.devze.com 2023-02-10 17:01 出处:网络
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 insta

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*, ve开发者_运维百科ctor<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.


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;


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消