Pair inside priority queue

牧云@^-^@ 提交于 2019-12-03 02:25:44

问题


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’

What could be the reason.I am new to STL.


回答1:


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;

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;

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.




回答2:


priority_queue<pair<int,int>,vector<pair<int,int>>,CompareDist> pq;

you need to provide second argument for the inbuilt template of priority_queue.



来源:https://stackoverflow.com/questions/12685787/pair-inside-priority-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!