在优先级队列中配对 [英] Pair inside priority queue
问题描述
我尝试在优先级队列中存储对,我使用比较每对的第二个值的比较函数。
I am trying to store pairs in priority queue and I am using a compare function that compares second value of each pair.
#include<iostream>
#include<queue>
#include<utility>
using namespace std;
class CompareDist
{
public:
bool operator()(pair<int,int> n1,pair<int,int> n2) {
return n1.second>n2.second;
}
};
int main()
{
priority_queue<pair<int,int>,CompareDist> pq;
}
当我编译这个文件时会出现错误
When I compile this I get an error
error: no type named ‘value_type’ in ‘class CompareDist’
$ b $
推荐答案
这是 href =http://en.cppreference.com/w/cpp/container/priority_queue> priority_queue 如下所示:
This is what priority_queue looks like:
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class priority_queue;
换句话说, CompareDist
第三参数,第二个参数应为容器(其具有 value_type
),如下所示:
In other words, CompareDist
should be the third argument and the second argument should be the container (which has value_type
), like the following:
priority_queue<pair<int,int>,vector<pair<int,int>>,CompareDist> pq;
注意, priority_queue
称为容器适配器。另一个容器用作底层容器,priority_queue具有访问它的特殊成员函数。容器适配器的另一个示例是std :: stack。
Notice also, that priority_queue
is what is called a "container adaptor". Another container is used as the underlying container and the priority_queue has special members functions for accessing it. Another example of a container adaptor would be std::stack.
这篇关于在优先级队列中配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!