C ++如何将参数传递给STL函数

问题描述 投票:1回答:1

我不得不将无序映射传递给优先级队列的比较器功能,并使用链接Passing a parameter to a comparison function?,我决定如下进行操作:

 priority_queue < int, std::vector<int>, compare(freq) > pq;
 struct compare
    {
        compare( std::unordered_map<int,int>& freq1 )
        {
            freq = freq1;
        }

        bool operator()( int& el1, int& el2 ){
            return freq[el1] < freq[el2];
        }    
        std::unordered_map<int,int> freq;
    };

但是我遇到错误:

Template argument for template type parameter must be a type

我在做什么错?

c++ stl comparator
1个回答
1
投票

正如错误消息所述,compare(freq)不是类型,不能将其指定为类型模板参数。

您应将compare(freq)指定为constructor of priority_queue的参数,并指定priority_queue作为类型模板的参数。

compare
© www.soinside.com 2019 - 2024. All rights reserved.