简体   繁体   English

使用更大和向量声明优先级队列

[英]Declaration of Priority Queue using greater and vector

Can anybody please explain what is the meaning of this declaration: 谁能解释一下这个声明的含义:

typedef pair<long long, int> PII;
priority_queue<PII, vector<PII>, greater<PII> > Q;

Is it to be treated as a priority queue of pair of long long and int or something else? 是否将其作为long longint或其他对象对的优先级队列? Can please someone also explain what is greater<PII> here? 请问有人也可以在这里解释什么是greater<PII>吗?

This declares a std::priority_queue containing std::pair<long long, int> instances where a std::vector<std::pair<long long, int>> is the underlying container of the priority queue (as it is a container adaptor). 这声明了一个包含std::pair<long long, int>实例的std::priority_queue std::pair<long long, int>其中std::vector<std::pair<long long, int>>是优先级队列的基础容器(因为它是容器适配器)。

The std::greater<std::pair<long long, int>> is used as the Comparator function object for the queue, checking whether the LHS pair is greater than the RHS pair. std::greater<std::pair<long long, int>>用作队列的比较器函数对象,检查LHS对是否大于RHS对。 See below for a reference: 请参阅下面的参考:

http://en.cppreference.com/w/cpp/utility/functional/greater http://en.cppreference.com/w/cpp/utility/functional/greater

Using greater here means that lower values are considered of higher priority and come out of the priority queue earlier. 在此处使用greater值意味着较低的值被认为具有较高的优先级,并且较早退出优先级队列。 By default less is used, and higher values are higher priority. 默认情况下,使用less值,而较高的值是较高的优先级。

Pairs are ordered lexicographically; 配对按字典顺序排列; (1, 3) comes before (2, 1) and after (1, 2). (1,3)在(2,1)之前和之后(1,2)。

This is way to create min heap in c++ . 这是在c ++中创建最小堆的方法。

typedef pair<long long, int> PII;
priority_queue<PII, vector<PII>, greater<PII> > Q;

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

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