简体   繁体   English

C ++ STL容器优先级队列初始化

[英]C++ STL container priority queue initialisation

from http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/ 来自http://www.cplusplus.com/reference/queue/priority_queue/priority_queue/

//initialize (1)    
explicit priority_queue (const Compare& comp = Compare(),
                         const Container& ctnr = Container());


//range (2) 
template <class InputIterator>
         priority_queue (InputIterator first, InputIterator last,
                         const Compare& comp = Compare(),
                         const Container& ctnr = Container());

Based on answers to similar question on SO, I am using 基于类似问题的答案,我正在使用

priority_queue<int, vector<int>, greater<int> > pq

However, this definition doesn't match with either 1 or 2 given in the reference site. 但是,此定义与参考站点中给定的1或2不匹配。 So how is this initialisation working? 那么这个初始化如何工作?

You're only looking at the reference for the constructor. 您只在查看构造函数的参考。

The template parameter list you need to look at is the template parameter list for the class template itself : 您需要查看的模板参数列表是类模板本身的模板参数列表:

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

You're instantiating a std::priority_queue with all three template parameters given explicitly, and doing so using the first constructor (which is not a function template — the second constructor is) and taking the defaults of both its arguments. 您要实例化一个std::priority_queue ,同时显式地指定所有三个模板参数,并使用第一个构造函数(这不是函数模板,第二个构造函数是)并采用其两个参数的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM