[英]C++ priority queue constructor
I found the constructor of priority_queue on http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/ is like this : 我在http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/上发现了priority_queue的构造函数,如下所示:
priority_queue (const Compare& comp, const Container& ctnr);
but the example I found is like this: 但是我发现的示例是这样的:
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
What's the difference between these two constructors? 这两个构造函数有什么区别?
I have tried both of them on my own, but the first one didn't work, the priority_queue wasn't sorted from small to large. 我自己尝试了这两个方法,但是第一个方法不起作用,priority_queue的大小不一。 Here is the code: 这是代码:
priority_queue<greater<int>, vector<int>> pq;
pq.push(4);
pq.push(2);
pq.push(1);
pq.push(3);
pq.push(5);
for (int i = 0; i < 5; i++) {
cout << pq.top() << endl;
pq.pop();
}
The result is still 5, 4, 3, 2, 1 结果仍然是5、4、3、2、1
What's the difference between these two constructors? 这两个构造函数有什么区别?
One of them is a constructor; 其中之一是构造函数。 the other is not. 另一个不是。
The line beginning typedef
just creates a type alias, called mypq_type
. 以typedef
开头的行仅创建一个类型别名,称为mypq_type
。 You would still pass those same constructor arguments when you create an object of this mypq_type
type. 创建此mypq_type
类型的对象时,仍将传递相同的构造函数参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.